我希望将数据框像这样转换为长格式
这是我使用的代码
long_ex <- melt(wide_ex, id.vars = 5, na.rm=TRUE) #wide_ex is wide format dataframe
但是,我的最终结果看起来像这样
有没有一种方法可以使用melt而不提取列名称?如果熔体最不适合这项工作,将很乐意接受替代功能
编辑:数据输出从dput
structure(list(ï..Column1 = c(NA, NA, NA, NA), Column2 = c(NA,
NA, NA, NA), Column3 = c(NA, NA, NA, NA), Column4 = c(NA, NA,
NA, NA), Column5 = structure(c(2L, 1L, 4L, 3L), .Label = c("Eric ",
"Jim", "Matt", "Tim"), class = "factor"), Column6 = c(NA, NA,
NA, NA), Column7 = structure(c(1L, 3L, 2L, 3L), .Label = c("Eric",
"Erica", "Mary "), class = "factor"), Column8 = structure(c(3L,
2L, 1L, 3L), .Label = c("Beth", "Loranda", "Matt"), class = "factor"),
Column9 = structure(c(2L, 3L, 1L, 3L), .Label = c("Courtney ",
"Heather ", "Patrick"), class = "factor"), Column10 = structure(4:1, .Label = c("Beth",
"Heather", "John", "Loranda "), class = "factor"), Column11 = c(NA,
NA, NA, NA), Column12 = c(NA, NA, NA, NA), Column13 = c(NA,
NA, NA, NA), Column14 = c(NA, NA, NA, NA), Column15 = c(NA,
NA, NA, NA)), class = "data.frame", row.names = c(NA, -4L
))
如果您需要基本的R解决方案:
data.frame(name_1 = rep(as.character(wide_ex$Column5), each=nrow(wide_ex)),
name_2 = as.vector(t(wide_ex[, c("Column7", "Column8", "Column9", "Column10")])))
我仍然认为最简洁的方法是与data.table融合:
library(data.table)
setDT(wide_ex)
melt(wide_ex, id.vars = c("Column5"), na.rm=TRUE)[,variable := NULL][]
如果需要关注速度,它将比reshape2显着提高速度。
(附加说明)...附加使用是[]
什么?
A) the use of additional [] in data.table
is known as chaining. It allows you to perform more operations on preceding []'s.
As you originally indicated, the output of melt produces an unwanted column (variable
). variable := NULL
removes it. It's essentially the same as doing the following (on your original question):
long_ex <- melt(wide_ex, id.vars = 5, na.rm=TRUE)
long_ex$variable <- NULL
However the use of :=
does it by reference (and on the same line).
Chaining can be super useful to keep your code nice and concise. Say you wanted to order your output by the first column (as you kind of indicated in your original question). You could do this like so:
melt(wide_ex, id.vars = c("Column5"), na.rm=TRUE)[, variable := NULL][order(Column5)][]
data.table确实是一个了不起的程序包(尤其是在处理中型到大型数据时)。如果您有兴趣,建议您阅读并了解更多信息:https : //github.com/Rdatatable/data.table/wiki
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句