简体   繁体   中英

Error in wide to long format in r reshape2 package

Hi I am new in R programming.

I want to change a list of data from wide to long format but the error comes out as follows:

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

change to:

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

The code used by me is as follows:

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)

The errors that come out are:

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

Thank you for your help.

If we are using a list of data.frames do the melt within each list element.

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

It is not clear whether the OP wanted to write the output in a single file or multiple files.

If it is a single file, rbind , the list of data.frames to a single one and then use write.csv

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

But, if this goes as separate files, then loop through the names of the 'dfN' and write it

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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