过滤并将多个维度列表列表转换为矩阵

时间:2014-12-27 10:12:41

标签: r matrix type-conversion nested-lists

我使用fromJSON()将JSON数据从Web服务提取到R中。

此函数将数据返回到多维R列表(x)。

我的目标是使用持续时间$ value将此列表转换为$行的x,y矩阵。

我以为我可以过滤列表以返回持续时间$ value,然后使用类似

的内容
output <- matrix(unlist(x$rows), ncol = 4, byrow = TRUE)

过滤列表列表以返回持续时间$ value的最佳方法是什么?

dput(x$rows)

    list(structure(list(elements = list(structure(list(distance = structure(list(
    text = "1 m", value = 0), .Names = c("text", "value")), duration = structure(list(
    text = "1 min", value = 0), .Names = c("text", "value")), 
    status = "OK"), .Names = c("distance", "duration", "status"
)), structure(list(distance = structure(list(text = "27.0 km", 
    value = 26970), .Names = c("text", "value")), duration = structure(list(
    text = "5 hours 25 mins", value = 19487), .Names = c("text", 
"value")), status = "OK"), .Names = c("distance", "duration", 
"status")), structure(list(distance = structure(list(text = "59.4 km", 
    value = 59410), .Names = c("text", "value")), duration = structure(list(
    text = "12 hours 11 mins", value = 43836), .Names = c("text", 
"value")), status = "OK"), .Names = c("distance", "duration", 
"status")), structure(list(distance = structure(list(text = "43.0 km", 
    value = 42976), .Names = c("text", "value")), duration = structure(list(
    text = "8 hours 45 mins", value = 31526), .Names = c("text", 
"value")), status = "OK"), .Names = c("distance", "duration", 
"status")))), .Names = "elements"), structure(list(elements = list(
    structure(list(distance = structure(list(text = "27.0 km", 
        value = 26970), .Names = c("text", "value")), duration = structure(list(
        text = "5 hours 25 mins", value = 19490), .Names = c("text", 
    "value")), status = "OK"), .Names = c("distance", "duration", 
    "status")), structure(list(distance = structure(list(text = "1 m", 
        value = 0), .Names = c("text", "value")), duration = structure(list(
        text = "1 min", value = 0), .Names = c("text", "value"
    )), status = "OK"), .Names = c("distance", "duration", "status"
    )), structure(list(distance = structure(list(text = "55.5 km", 
        value = 55488), .Names = c("text", "value")), duration = structure(list(
        text = "11 hours 27 mins", value = 41231), .Names = c("text", 
    "value")), status = "OK"), .Names = c("distance", "duration", 
    "status")), structure(list(distance = structure(list(text = "19.7 km", 
        value = 19744), .Names = c("text", "value")), duration = structure(list(
        text = "4 hours 4 mins", value = 14629), .Names = c("text", 
    "value")), status = "OK"), .Names = c("distance", "duration", 
    "status")))), .Names = "elements"), structure(list(elements = list(
    structure(list(distance = structure(list(text = "59.4 km", 
        value = 59354), .Names = c("text", "value")), duration = structure(list(
        text = "12 hours 7 mins", value = 43640), .Names = c("text", 
    "value")), status = "OK"), .Names = c("distance", "duration", 
    "status")), structure(list(distance = structure(list(text = "55.5 km", 
        value = 55487), .Names = c("text", "value")), duration = structure(list(
        text = "11 hours 24 mins", value = 41039), .Names = c("text", 
    "value")), status = "OK"), .Names = c("distance", "duration", 
    "status")), structure(list(distance = structure(list(text = "1 m", 
        value = 0), .Names = c("text", "value")), duration = structure(list(
        text = "1 min", value = 0), .Names = c("text", "value"
    )), status = "OK"), .Names = c("distance", "duration", "status"
    )), structure(list(distance = structure(list(text = "39.1 km", 
        value = 39111), .Names = c("text", "value")), duration = structure(list(
        text = "7 hours 58 mins", value = 28703), .Names = c("text", 
    "value")), status = "OK"), .Names = c("distance", "duration", 
    "status")))), .Names = "elements"), structure(list(elements = list(
    structure(list(distance = structure(list(text = "43.0 km", 
        value = 42976), .Names = c("text", "value")), duration = structure(list(
        text = "8 hours 48 mins", value = 31656), .Names = c("text", 
    "value")), status = "OK"), .Names = c("distance", "duration", 
    "status")), structure(list(distance = structure(list(text = "19.7 km", 
        value = 19744), .Names = c("text", "value")), duration = structure(list(
        text = "4 hours 5 mins", value = 14694), .Names = c("text", 
    "value")), status = "OK"), .Names = c("distance", "duration", 
    "status")), structure(list(distance = structure(list(text = "39.1 km", 
        value = 39113), .Names = c("text", "value")), duration = structure(list(
        text = "8 hours 3 mins", value = 28957), .Names = c("text", 
    "value")), status = "OK"), .Names = c("distance", "duration", 
    "status")), structure(list(distance = structure(list(text = "1 m", 
        value = 0), .Names = c("text", "value")), duration = structure(list(
        text = "1 min", value = 0), .Names = c("text", "value"
    )), status = "OK"), .Names = c("distance", "duration", "status"
    )))), .Names = "elements"))

1 个答案:

答案 0 :(得分:2)

尝试

sapply(x$rows, function(.x) sapply(.x$elements,
                function(y) y[['duration']]$value))

#      [,1]  [,2]  [,3]  [,4]
#[1,]     0 19490 43640 31656
#[2,] 19487     0 41039 14694
#[3,] 43836 41231     0 28957
#[4,] 31526 14629 28703     0

或者你可以这样做:

x2 <- unlist(x$rows)
matrix(as.numeric(unname(x2[grep('duration.value',
                               names(x2))])),ncol=4)
 #     [,1]  [,2]  [,3]  [,4]
 #[1,]     0 19490 43640 31656
 #[2,] 19487     0 41039 14694
 #[3,] 43836 41231     0 28957
 #[4,] 31526 14629 28703     0