获取嵌套列表中元素的长度

时间:2018-06-26 18:43:38

标签: r dataframe

我有一个来自JSON(jsonlite)的列表(包含列表),像这样的列表(以下为dput

{
    "1":["123", "131", "342"],
    "2":["123", "131"],
    "3":["123", "131", "352"],
    "4":["31", "352"],
    "5":["153", "131"],
    "6":["153", "131", "382"]
}

structure(list(`1` = c("123", "131", "342"), `2` = c("123", "131" ), `3` = c("123", "131", "352"), `4` = c("31", "352"), `5` = c("153", "131"), `6` = c("153", "131", "382")), .Names = c("1", "2", "3", "4", "5", "6"))

然后,我正在尝试使用键和嵌套列表的长度将其转换为数据框,例如

  V1 V2 V3 V4 V5 V6
1  1  2  3  4  5  6
2  3  2  3  2  2  3

带有该代码:

 a = (read_file("ghist.json") %>% fromJSON)$hist # Reads my list from a JSON file
 dates = data.frame() #Creates an empty data frame

 #Iterate my list element by element
 for(i in 1:length(a)){
   dates[1, i] = strtoi(names(a)[i]) #Appends to my data frame on the first row, line 'i' the key from my list (index 'i'), as Integer
   dates[2, i] = length(a[i]) #Here is my problem, it returns '1', not the real length of my list (index 'i')
 }
 print(dates) #Just debug

有了上面的代码,我得到了

  V1 V2 V3 V4 V5 V6
1  1  2  3  4  5  6
2  1  1  1  1  1  1

注意::我知道密钥上的数字只是新月形的数字,但是它将变成毫秒为单位的日期

2 个答案:

答案 0 :(得分:2)

您可以只使用内置的lengths函数来构造数据框。这使您可以使用列表元素的长度,而这正是您想要的。

a <- structure(list(`1` = c("123", "131", "342"), `2` = c("123", "131"), `3` = c("123", "131", "352"), `4` = c("31", "352"), `5` = c("153", "131"), `6` = c("153", "131", "382")), .Names = c("1", "2", "3", "4", "5", "6"))

dates <-  data.frame(
  matrix(
    data = c(names(a), lengths(a)),
    ncol = length(a),
    byrow = TRUE
    )
  )
dates
#>   X1 X2 X3 X4 X5 X6
#> 1  1  2  3  4  5  6
#> 2  3  2  3  2  2  3

您代码中的错误非常小,以为我不推荐这种方法。这是您需要length(a[[i]])。我建议您look at some resources on subsetting in R,但为便于说明,请在底部进行比较。 a[1]返回包含所需元素的列表,a[[1]]返回实际元素。 a[1]的长度为1。

dates = data.frame()
for(i in 1:length(a)){
  dates[1, i] = strtoi(names(a)[i]) 
  dates[2, i] = length(a[[i]]) # changed here
}
dates
#>   V1 V2 V3 V4 V5 V6
#> 1  1  2  3  4  5  6
#> 2  3  2  3  2  2  3

a[1]
#> $`1`
#> [1] "123" "131" "342"
a[[1]]
#> [1] "123" "131" "342"

reprex package(v0.2.0)于2018-06-26创建。

答案 1 :(得分:-1)

在循环中尝试使用nrow(a [i])而不是length(a [i])。