简体   繁体   English

在长格式数据框中使用不带列名的熔化将宽格式转换为长格式

[英]Convert wide to long format using melt without column name in long format dataframe

I have a dataframe that looks like this 我有一个看起来像这样的数据框 在此处输入图片说明

I want the dataframe to be converted to long format like this 我希望将数据框像这样转换为长格式

在此处输入图片说明

This is the code I use 这是我使用的代码

long_ex <- melt(wide_ex, id.vars = 5, na.rm=TRUE) #wide_ex is wide format dataframe

However, my end result looks like this 但是,我的最终结果看起来像这样

在此处输入图片说明

Is there a way to use melt without extracting the column names? 有没有一种方法可以使用melt而不提取列名称? Will gladly accept alternative functions if melt is not best suited for this job 如果熔体最不适合这项工作,将很乐意接受替代功能

Edit: Data output from dput 编辑:数据输出从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
))

If you want a base R solution: 如果您需要基本的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")])))

I'm still of the opinion the most concise method is melt with data.table: 我仍然认为最简洁的方法是与data.table融合:

library(data.table)
setDT(wide_ex)
melt(wide_ex, id.vars = c("Column5"), na.rm=TRUE)[,variable := NULL][]

It will also offer considerable speed improvements over reshape2 if speed is of concern. 如果需要关注速度,它将比reshape2大大提高速度。



(Additional explanation) ... What's with the use of additional [] ? (附加说明)... 附加[]的用法是什么?

A) the use of additional [] in data.table is known as chaining. A)在data.table使用额外的[]被称为链接。 It allows you to perform more operations on preceding []'s. 它允许您对前面的[]执行更多操作。

As you originally indicated, the output of melt produces an unwanted column ( variable ). 正如您最初指出的那样,熔体的输出会产生不需要的列( variable )。 variable := NULL removes it. variable := NULL将其删除。 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)][] 融化(wide_ex,id.vars = c(“ Column5”),na.rm = TRUE)[,变量:= NULL] [order(Column5)] []

data.table really is an amazing package (especially if you're dealing with medium to large data). data.table确实是一个了不起的程序包(尤其是在处理中大型数据时)。 If you're interested, I'd suggest reading & learning more about it: https://github.com/Rdatatable/data.table/wiki 如果您有兴趣,我建议您阅读并了解有关它的更多信息: https : //github.com/Rdatatable/data.table/wiki

Similarly using melt for a tidyverse approach 类似地,将melt用于tidyverse方法

library(tidyverse)
library(data.table)

df %>%
  melt(id.vars=5) %>%
  filter(complete.cases(.)) %>%
  select(c(1,3))

 Column5     value
1    Jim     Eric
2    Eric    Mary 
3    Tim     Erica
4    Matt    Mary 
5    Jim     Matt
6    Eric    Loranda
7    Tim     Beth
8    Matt    Matt
9    Jim     Heather 
10   Eric    Patrick
11   Tim     Courtney 
12   Matt    Patrick
13   Jim     Loranda 
14   Eric    John
15   Tim     Heather
16   Matt    Beth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM