r reshape2 包中宽格式到长格式的错误

Error in wide to long format in r reshape2 package

嗨,我是 R 编程的新手。

我想将数据列表从宽格式更改为长格式,但出现如下错误:

EA    UU    AR
0.455 1.106 0.568
1.406 0.710 0.262
1.124 1.406 0.312

更改为:

EA 0.455
EA 1.406
EA 1.124
UU 1.106
UU 0.710
UU 1.406
AR 0.568
AR 0.262
AR 0.312

我使用的代码如下:

files <- list.files(path = ".", pattern = "*.txt", all.files = TRUE, full.names = FALSE, recursive = FALSE)

listy <- lapply(files, read.csv)

melt(listy)

write.table(melt(listy), file = "listofdata.txt", quote = FALSE, sep = " ", row.names = FALSE, col.names = FALSE)

出现的错误是:

No id variables; using all as measure variables

No id variables; using all as measure variables

No id variables; using all as measure variables

No id variables; using all as measure variables

No id variables; using all as measure variables

No id variables; using all as measure variables

感谢您的帮助。

如果我们使用 data.frameslist,请在每个 list 元素中执行 melt

 listN <- lapply(listy, function(x) melt(x))

不清楚 OP 是想将输出写入单个文件还是多个文件。

如果是单个文件,rbind,把data.frameslist改成单个,然后用write.csv

 dfN <- do.call(rbind, listN)
 write.csv(dfN, "dataLong.csv", row.names=FALSE, quote=FALSE)

但是,如果这作为单独的文件,则循环遍历 'dfN' 的 names 并写入它

 lapply(names(listN), function(nm) write.csv(listN[[nm]], 
                 paste0(nm, ".csv", row.names=FALSE, quote=FALSE)