簡體   English   中英

R中具有不同行長度的CSV列表

[英]List with different row lengths to CSV in R

我有一個看起來像這樣的列表:

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類型並且來自不同的長度。

我試過尋找一些像這樣的解決方案 或調用此行:

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

但是.csv文件沒有數據,而是每種類型的信息,長度是每一列。

我嘗試調用此行: do.call("rbind", lapply(data, as.data.frame))並且出現以下錯誤

(function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : 參數暗示不同的行數:

那么,有沒有人知道我該如何解決這個問題?

編輯所以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;

更新了較新的數據。

您已在問題中將其格式化為帶有列表列的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="|")這里的視覺效果和任何東西一樣多。 請注意,只要您有正常的引用,就可以對兩者都使用"," ,它會正確解析……盡管肉眼很難看出區別。

     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 ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM