Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.2k views
in Technique[技术] by (71.8m points)

export - Exporting an R Spread Table

I'm new to R, but have had some initial success grabbing data (gwlevel) and then using the "spread function" to put into a spread table (gwlevel_spread).

My problem is that I can easily export the "gwlevel" table, as follows:

write.table(gwlevel, file = "c:/BOB/r/export/zz_jnk.csv")

However, I keep getting an error when I try to export the "gwlevel_spread" file, as follows:

write.table(gwlevel_spread,"c:/BOB/r/export/zz_jnk.csv",sep=",") Error in write.table(gwlevel_spread, "c:/BOB/r/export/zz_jnk.csv", sep = ",") : unimplemented type 'list' in 'EncodeElement'

(1) Can anybody tell me the reason? (2) Or is there a better way I should be doing it?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

That probably means that you have some list columns in your data which cannot be exported to csv. You can collapse the list column to one comma-separated string before writing it to csv.

If the list column is called col you can do :

gwlevel_spread$col <- sapply(gwlevel_spread$col, toString)

If you don't know which column is list or have many columns that are of list class in your data you can use condition in lapply

gwlevel_spread[] <- lapply(gwlevel_spread, function(x) 
                          if('list' %in% class(x)) sapply(x, toString) else x)

and then write to csv using write.table or write.csv :

write.table(gwlevel_spread, file = "c:/BOB/r/export/zz_jnk.csv")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...