r reshape2包中从长到长格式出错

时间:2016-04-28 03:18:14

标签: r format reshape2

嗨,我是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

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

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

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

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

如果是单个文件,rbindlist data.frames为一个文件,然后使用write.csv

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

但是,如果这是单独的文件,那么循环遍历&#39; dfN&#39;的names。写下来

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