【问题标题】:List with different row lengths to CSV in R在R中具有不同行长度的列表到CSV
【发布时间】:2020-10-07 17:25:28
【问题描述】:

我的列表如下所示:

City | Country | TrainArrivals  
A | country_1 | 8.00, 9.30, 10.00, 15.15  
B | country_1 | 11.00, 12.30, 18.00, 22.20, 22.50  
C | country_2 | 8.10, 11.20, 13.00, 16.40, 19.20, 23.00 

所以它全部保存为一个列表(称为data)。这里我必须指出data$TrainArrivals 也是list 类型并且长度不同。

我已尝试寻找一些解决方案like this one. 或致电此行:

capture.output(summary(data), file = paste(path, "values.csv", sep = "/"))    

但是.csv 文件没有数据,而是信息的类型,长度是每一列。

我尝试调用此行:do.call("rbind", lapply(data, as.data.frame)),但出现以下错误

错误(函数(...,row.names = NULL,check.rows = FALSE, check.names = TRUE, : 参数暗示不同的行数:

那么,有人知道我该如何解决这个问题吗?

EDIT 所以 dput(data)

的输出
    structure(list(scenario = "first", pr = "all", rep = "2", 
    plot_data = list(c(81677L, 91437L, 233376L, 71580L, 43126L, 
    28724L, 15453L, 11162L, 8355L, 6786L, 5756L, 5162L, 4473L, 
    3848L, 3617L, 3331L, 2941L, 2572L, 2289L, 1974L, 1797L, 1575L, 
    1325L, 1217L, 1012L, 886L, 787L, 709L, 548L, 409L, 399L, 
    339L, 292L, 215L, 128L, 113L, 83L, 61L, 42L, 30L, 18L, 15L, 
    6L, 12L, 4L, 1L, 0L, 1L, 1L, 0L, 1L))), .Names = c("first", 
"pr", "rep", "plot_data"), row.names = c(NA, -1L), groups = structure(list(
    scenario = "first", pr = "all", .rows = structure(list(
        1L), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", 
    "list"))), .Names = c("scenario", "pr", ".rows"), row.names = 1L, class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

期望的输出

City; Country; trainArrivals;  
A;country_1;8.00, 9.30, 10.00, 15.15;
B;country_1;11.00, 12.30, 18.00, 22.20, 22.50;  
C;country_2;8.10, 11.20, 13.00, 16.40, 19.20, 23.00;

【问题讨论】:

  • list,还是带有列表列的data.frame?如果您在此处提供 dput(data) 的输出,可能会有很大帮助。
  • @r2evans 我有一个问题,我为我的实际问题做了一个简单的例子。当我尝试发布原始问题的 dput 时,我得到“正文限制为 30000 个字符;您输入了 36646。”
  • dput(head(data,3))dput(data[1:3,1:4])?我们不需要所有的数据,我们真的需要足够来表达重点。 :-)
  • 好吧,我做了 dput(data[3,]),我认为应该可以解决问题

标签: r


【解决方案1】:

更新更新的数据。

您已在问题中将其格式化为带有列表列的data.frame,所以我将不再使用它。

几个选项:

  1. 存储为 json,以便任何语言立即获得正确的结构:

    writeLines(jsonlite::toJSON(dat), "dat.json")
    str( jsonlite::read_json("dat.json", simplifyDataFrame = TRUE) )
    # 'data.frame': 1 obs. of  4 variables:
    #  $ first    : chr "first"
    #  $ pr       : chr "all"
    #  $ rep      : chr "2"
    #  $ plot_data:List of 1
    #   ..$ : int  81677 91437 233376 71580 43126 28724 15453 11162 8355 6786 ...
    
  2. 将列表列折叠成易于撤消的内容。我将在这里使用collapse=",",尽管您可以使用任何已知不在数据中的字符。 (我发现"," 对其他用户来说很直观。)

    请注意,这会就地修改您的数据,因此如果您这样做,您可能希望在其临时副本上执行此操作,或者您需要在真实数据上手动撤消它。

    为了将嵌套列表分隔符与普通的表格字段分隔符区分开来,我将使用write.table(., sep="|"),就像在 here 中一样显示视觉效果。请注意,只要您有正常的引用,您就可以将"," 用于 both 并且它会正确解析......尽管眼睛很难看到区别。

    dat$plot_data <- sapply(dat$plot_data, paste, collapse = ",")
    write.table(dat, "dat.txt", sep = ";", row.names = FALSE, quote = FALSE)
    invisible(sapply(readLines("dat.txt"), cat, "\n"))
    # first;pr;rep;plot_data 
    # first;all;2;81677,91437,233376,71580,43126,28724,15453,11162,8355,6786,5756,5162,4473,3848,3617,3331,2941,2572,2289,1974,1797,1575,1325,1217,1012,886,787,709,548,409,399,339,292,215,128,113,83,61,42,30,18,15,6,12,4,1,0,1,1,0,1 
    
    newdat <- read.table("dat.txt", header = TRUE, sep = ";")
    newdat$plot_data <- lapply(strsplit(newdat$plot_data, "[,[:space:]]+"), as.integer)
    str(newdat)
    # 'data.frame': 1 obs. of  4 variables:
    #  $ first    : chr "first"
    #  $ pr       : chr "all"
    #  $ rep      : int 2
    #  $ plot_data:List of 1
    #   ..$ : int  81677 91437 233376 71580 43126 28724 15453 11162 8355 6786 ...
    

【讨论】:

  • 我已经用所需的输出更新了我的问题。我不确定您的建议是否相同
  • write.table(., sep=";", quote=FALSE)替换我的write.table(., sep="|")
猜你喜欢
  • 2013-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-02
  • 1970-01-01
  • 2020-05-25
  • 2012-02-03
相关资源
最近更新 更多