【问题标题】:How to save a dataframe with list to csv file in R如何将带有列表的数据框保存到R中的csv文件
【发布时间】:2016-08-19 13:39:39
【问题描述】:

不知道有没有可能……

我会保存文件以便在以后的其他会话中导入它。

我想将这个 data.frame 保存在一个 csv 文件中(它是一个包含向量的列表):

> operacions<-list(list(Nom="Victor",Bolis= c("Negro","azul","verde")),list(Nom="Dani",Lapices=c(1:4)))
> operacions
[[1]]
[[1]]$Nom
[1] "Victor"

[[1]]$Bolis
[1] "Negro" "azul"  "verde"


[[2]]
[[2]]$Nom
[1] "Dani"

[[2]]$Lapices
[1] 1 2 3 4

但它给了我一个错误:

> write.csv2(operacions, "file.csv",row.names = FALSE)
Error in data.frame(list(Nom = "Victor", Bolis = c("Negro", "azul", "verde" : 
  arguments imply differing number of rows: 3, 4

【问题讨论】:

  • 这不是一个数据框,这是一个列表列表。这里的问题是元素的长度不同。rapply(operacions, length) 返回1 3 1 4。这是一个问题,因为.csv 需要固定长度。输出的预期格式是什么?
  • 这可能会回答你的问题:stackoverflow.com/a/27594769/5805670

标签: r list csv arraylist dataframe


【解决方案1】:

R 数据框和 R 列表可能不兼容...

列表本质上是一个向量,其中每个元素可以是不同类型和长度的元素。同时,数据框可以被认为是一个统一的电子表格,其中每一列可以存储不同类型的数据,或者是非嵌套列表其中每个元素的长度相同。最后一点很重要,因为列表的长度可能不相等,而数据帧则不然。

因此,使用saveRDS() 将列表另存为 RDS 文件可能更容易

dataDir <- "."
saveRDS( operacions, file.path(dataDir, "files"))
operacions2 <- readRDS(file.path(dataDir, "files"))

这会将其保存为 R 对象,并且类似地稍后恢复 R 对象。

如果您确实要转换为数据框,可以先转换为嵌套数据框,然后填写数据框中的空白。于是……

require(data.table)
require(plyr)


operacions<-list(list(Nom="Victor",Bolis= c("Negro","azul","verde")),list(Nom="Dani",Lapices=c(1:4)))
str(operacions)
operacionsdf <- lapply(operacions, data.frame, stringsAsFactors = FALSE)
str(operacionsdf)

operacionsdf2 <- rbind.fill(operacionsdf)
str(operacionsdf2)
write.csv2(operacionsdf2, "file.csv",row.names = FALSE)
operacionsdf3 <- read.csv2("file.csv")
str(operacionsdf3)

输出:

> require(data.table)
> require(plyr)
> operacions<-list(list(Nom="Victor",Bolis= c("Negro","azul","verde")),list(Nom="Dani",Lapices=c(1:4)))
> str(operacions)
List of 2
 $ :List of 2
  ..$ Nom  : chr "Victor"
  ..$ Bolis: chr [1:3] "Negro" "azul" "verde"
 $ :List of 2
  ..$ Nom    : chr "Dani"
  ..$ Lapices: int [1:4] 1 2 3 4
> operacionsdf <- lapply(operacions, data.frame, stringsAsFactors = FALSE)
> str(operacionsdf)
List of 2
 $ :'data.frame':   3 obs. of  2 variables:
  ..$ Nom  : chr [1:3] "Victor" "Victor" "Victor"
  ..$ Bolis: chr [1:3] "Negro" "azul" "verde"
 $ :'data.frame':   4 obs. of  2 variables:
  ..$ Nom    : chr [1:4] "Dani" "Dani" "Dani" "Dani"
  ..$ Lapices: int [1:4] 1 2 3 4
> operacionsdf2 <- rbind.fill(operacionsdf)
> str(operacionsdf2)
'data.frame':   7 obs. of  3 variables:
 $ Nom    : chr  "Victor" "Victor" "Victor" "Dani" ...
 $ Bolis  : chr  "Negro" "azul" "verde" NA ...
 $ Lapices: int  NA NA NA 1 2 3 4
> write.csv2(operacionsdf2, "file.csv",row.names = FALSE)
> operacionsdf3 <- read.csv2("file.csv")
> str(operacionsdf3)
'data.frame':   7 obs. of  3 variables:
 $ Nom    : Factor w/ 2 levels "Dani","Victor": 2 2 2 1 1 1 1
 $ Bolis  : Factor w/ 3 levels "azul","Negro",..: 2 1 3 NA NA NA NA
 $ Lapices: int  NA NA NA 1 2 3 4

【讨论】:

    【解决方案2】:

    由于它不是 data.frame ,更具体地说,不能用 as.data.frame 强制转换为该消息的来源,因此您需要考虑另一种保存数据的方法。最简单的可能是dput,它编写了列表结构的 ASCII 表示:

    dput(operacions, file="out.txt")
    

    将其带回 R:

    new <- source("out.txt")
    

    另一种方法是转换为 JSON 格式,这也将保留键值信息,而不仅仅是写入值:

    library(jsonlite)
     toJSON(new)
    # value---------
    {"value":[{"Nom":["Victor"],"Bolis":["Negro","azul","verde"]},{"Nom":["Dani"],"Lapices":[1,2,3,4]}],"visible":[true]} 
    

    您可以使用 cat 函数将其定向到文本文件:

    cat( toJSON(new), file="test.3.txt")
    

    【讨论】:

      猜你喜欢
      • 2018-06-09
      • 2016-08-20
      • 2021-09-28
      • 1970-01-01
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      • 1970-01-01
      相关资源
      最近更新 更多