简体   繁体   English

从R中的列表创建csv文件

[英]Create csv file from a list in R

I have a data frame with the following variables: 我有一个带有以下变量的数据框:

   id<- c('A', 'B', 'C', 'll')
   location<- list(c('newyork', 'boston'), c('london','paris'), NULL, c('mumbai'))

  df<- data.frame(id, location)
  write.csv(df, file='test.csv')

I want to create a csv file from the data frame but i am getting error as the location is a list? 我想从数据框中创建一个csv文件,但是由于位置是列表,我遇到了错误? Any ideas on how i can do this in R? 关于如何在R中执行此操作的任何想法?

This sort of thing data structure was expected, if csv doesn't allow for nested structure it might be problematic. 这种事物数据结构是预料之中的,如果csv不允许嵌套结构,则可能会出现问题。 How about xls or xlsx? xls或xlsx呢?

  id location
  A  c(newyork, boston)
  B  c(london, paris)
  C  Null 
  D  c('mumbai')

Run paste to combine elements in location . 运行paste以在location组合元素。 Then everything can be the same. 然后一切都可以相同。

id<- c('A', 'B', 'C', 'll')
location<- list(c('newyork', 'boston'), c('london','paris'), NULL, c('mumbai'))
location = paste(location,sep = "")
df = data.frame(id,location)
write.csv(df, file='test.csv')
#> df
#  id               location
#1  A c("newyork", "boston")
#2  B   c("london", "paris")
#3  C                   NULL
#4 ll                 mumbai

The csv will look like below: csv将如下所示:

"","id","location"
"1","A","c(""newyork"", ""boston"")"
"2","B","c(""london"", ""paris"")"
"3","C","NULL"
"4","ll","mumbai"

You can cleanup further if you want 您可以根据需要进一步清理

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

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