I have a data.frame
with several columns:
set.seed(1)
df <- data.frame(cluster=LETTERS[1:4],group=c(rep("m",2),rep("f",2)),point=rnorm(4),err=runif(4,0.1,0.3))
and I'd add another columns which "\n"
concatenates
all columns of its respective row, where the column name precedes the value.
I know this:
library(tidyr)
library(dplyr)
tidyr::unite(df,text,sep="\n")
gives me this tibble
:
text
1 A\nm\n0.487429052428485\n0.286941046221182
2 B\nm\n0.738324705129217\n0.142428504256532
3 C\nf\n0.575781351653492\n0.230334753217176
4 D\nf\n-0.305388387156356\n0.125111019192263
But what I want is this tibble
:
text
1 cluster: A\ngroup: m\npoint: 0.487429052428485\nerr: 0.286941046221182
2 cluster: B\ngroup: m\npoint: 0.738324705129217\nerr: 0.142428504256532
3 cluster: C\ngroup: f\npoint: 0.575781351653492\nerr: 0.230334753217176
4 cluster: D\ngroup: f\npoint: -0.305388387156356\nerr: 0.125111019192263
Any idea?
How do I concatenate two columns in R? To concatenate two columns you can use the <code>paste()</code> function. For example, if you want to combine the two columns A and B in the dataframe df you can use the following code: <code>df['AB'] <- paste(df$A, df$B)</code>.
The unite() method is used to merge two or more columns into a single column or variable. unite() generates a single data frame as output after merging the specified columns.
To convert a column values to column names, we can use dcast function of reshape2 package. For example, if we have a data frame called df that contains two columns say x and y, where x is categorical and y is numerical. Now if we want to convert the categories in x as column names then it can be done as dcast(df,y~x).
To convert multiple columns into single column in an R data frame, we can use unlist function. For example, if we have data frame defined as df and contains four columns then the columns of df can be converted into a single by using data. frame(x=unlist(df)).
We can use Map
with do.call
data.frame(text = do.call(paste, c(Map(function(x, y)
paste(x, y, sep=": "), names(df), df), sep="\n")))
Or using tidyverse
, map through the columns (imap
- provides the .y
as column names) and then do the unite
library(tidyverse)
imap(df, ~ paste(.y, .x, sep=": ")) %>%
as_tibble %>%
unite(text, sep="\n")
# A tibble: 4 x 1
# text
# <chr>
#1 "cluster: A\ngroup: m\npoint: -0.626453810742332\nerr: 0.225822808779776"
#2 "cluster: B\ngroup: m\npoint: 0.183643324222082\nerr: 0.112357254093513"
#3 "cluster: C\ngroup: f\npoint: -0.835628612410047\nerr: 0.14119491497986"
#4 "cluster: D\ngroup: f\npoint: 1.59528080213779\nerr: 0.135311350505799"
Or as @DanChaltiel mentioned
imap_dfr(df, ~ paste(.y, .x, sep = "; ")) %>%
unite(text, sep = "\n")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With