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?
aggregate(do.call(paste,c(sep=" :",rev(stack(df)))),list(c(row(df))),paste,collapse="\n")
– Cleanup