我的数据如下
df=data.frame(
Id=c("001","002","003","004"),
author=c('John Cage','Thomas Carlyle'),
circa=c('1988', '1817'),
quote=c('I cant understand why people are frightened of new ideas. Im frightened of the old ones.',
'My books are friends that never fail me.')
)
df
我想合并3列以获得下面的数据框
df2 = data.frame(
Id=c("001","002"),
text = c(
'Author:
John Cage
Circa:
1988
quote:
I cant understand why people are frightened of new ideas. Im frightened of the old ones.
',
'Author:
Thomas Carlyle
Circa:
1817
quote:
My books are friends that never fail me.
'
)
)
df2
我知道我可以使用 tidyr 中的 paste 或 unite ,但是如何将列名传递到新创建的列中专栏?
答案 0 :(得分:1)
这里是base R
的解决方案,其中使用了paste0()
。也许以下代码可以帮助您实现
res <- cbind(df[1],text = apply(apply(df[-1], 1, function(v) paste0(names(df[-1]),": ",v)), 2, paste0, collapse = "\n"))
如此
> res
Id text
1 001 author: John Cage\ncirca: 1988\nquote: I cant understand why people are frightened of new ideas. Im frightened of the old ones.
2 002 author: Thomas Carlyle\ncirca: 1817\nquote: My books are friends that never fail me.
数据
df <- structure(list(Id = structure(1:2, .Label = c("001", "002"), class = "factor"),
author = structure(1:2, .Label = c("John Cage", "Thomas Carlyle"
), class = "factor"), circa = structure(2:1, .Label = c("1817",
"1988"), class = "factor"), quote = structure(1:2, .Label = c("I cant understand why people are frightened of new ideas. Im frightened of the old ones.",
"My books are friends that never fail me."), class = "factor")), class = "data.frame", row.names = c(NA,
-2L))
答案 1 :(得分:1)
您可以获取长格式的数据,然后按组paste
。
library(dplyr)
df %>%
tidyr::pivot_longer(cols = -Id) %>%
group_by(Id) %>%
summarise(text = paste(name, value, sep = ":", collapse = "\n"))
# A tibble: 4 x 2
# Id text
# <fct> <chr>
#1 001 "author:John Cage\ncirca:1988\nquote:I cant understand why people are f…
#2 002 "author:Thomas Carlyle\ncirca:1817\nquote:My books are friends that nev…
#3 003 "author:John Cage\ncirca:1988\nquote:I cant understand why people are f…
#4 004 "author:Thomas Carlyle\ncirca:1817\nquote:My books are friends that nev…
答案 2 :(得分:1)
我们可以在melt
中使用data.table
library(data.table)
melt(setDT(df), id.var = 'Id')[, .(text = paste(variable,
value, sep=":", collapse="\n")), Id]
# Id text
#1: 001 author:John Cage\ncirca:1988\nquote:I cant understand why people are frightened of new ideas. Im frightened of the old ones.
#2: 002 author:Thomas Carlyle\ncirca:1817\nquote:My books are friends that never fail me.