R 中的嵌套分层数据框

Nested, hierarchical data frames in R

我是 R 的新手,我不想从一开始就误解语言及其数据结构。 :)

我的 data.frame sample.data 包含 'normal' 属性(例如 author)另一个 data.frame 的嵌套列表(files),其中有例如属性 extension.

如何筛选创建了具有特定扩展名的文件的作者?有 R-ic 的方法吗?也许在这个方向:

t <- subset(data, data$files[['extension']] > '.R')

其实我想避免for循环。

在这里您可以找到一些示例数据:

d1 <- data.frame(extension=c('.py', '.py', '.c++')) # and some other attributes
d2 <- data.frame(extension=c('.R', '.py')) # and some other attributes

sample.data <- data.frame(author=c('author_1', 'author_2'), files=I(list(d1, d2)))

JSON sample.data 来自 貌似

[
    {
        "author": "author_1",
        "files": [
            {
                "extension": ".py",
                "path": "/a/path/somewhere/"
            },
            {
                "extension": ".c++",
                "path": "/a/path/somewhere/else/"
            }, ...
        ]
    }, ...
]

我想 base 包中的 grep() 函数可能是您的解决方案:

files <- data.frame(path = paste0("path", 1:3), extension = c (".R", ".csv", ".R")
                    , creation.date = c(Sys.Date()+1:3))

> files
# path extension creation.date
# 1 path1        .R    2015-07-15
# 2 path2      .csv    2015-07-16
# 3 path3        .R    2015-07-17


> files[grep(".R", files$extension),]
# extension creation.date
# 1 path1        .R    2015-07-15
# 3 path3        .R    2015-07-17

假设您的数据框 df 为 CSV,如下所示:

author,path,extension
john,/home/john,txt
mary,/home/mary,png

那么最简单的解决方案就是使用 dplyr 包:

library(dplyr)
filter(df, author=="john" & extension=="txt") 

至少有十几种方法可以做到这一点,但如果你想正确地学习 R,你应该学习子集数据结构的标准方法,尤其是原子向量、列表和数据框。这在本书的第二章中有所介绍:

http://adv-r.had.co.nz/

还有其他好书,但这是一本好书,而且是在线免费的。

更新: 好的,这会将您的 json 转换为数据帧列表。

library("rjson")
s <- paste(c(
'[{' ,
'  "author": "author_1",',
'  "files": [',
'    {',
'     "extension": ".py",',
'     "path": "/a/path/somewhere/"',
'   },',
'   {',
'     "extension": ".c++",',
'     "path": "/a/path/somewhere/else/"',
'    }]',
'},',
'{',
'"author": "author_2",',
'"files": [',
'  {',
'    "extension": ".py",',
'    "path": "/b/path/somewhere/"',
'  },',
'  {',
'    "extension": ".c++",',
'    "path": "/b/path/somewhere/else/"',
'  }]',
'}]'),collapse="")

j <- fromJSON(s)

todf <- function (x) {
    nrow <- length(x$files)
    vext <- sapply(x$files,function (y) y[[1]])
    vpath <- sapply(x$files,function (y) y[[2]])
    df <- data.frame(author=rep(x$author,nrow),ext=vext,path=vpath)
}
listdf <- lapply(j,todf)
listdf

产生:

[[1]]
    author  ext                    path
1 author_1  .py      /a/path/somewhere/
2 author_1 .c++ /a/path/somewhere/else/

[[2]]
    author  ext                    path
1 author_2  .py      /b/path/somewhere/
2 author_2 .c++ /b/path/somewhere/else/

并完成任务、合并和子集:

   mdf <- do.call("rbind", listdf)
   mdf[ mdf$ext==".py", ]

产量:

    author ext               path
1 author_1 .py /a/path/somewhere/
3 author_2 .py /b/path/somewhere/

有意思,用R模拟分层数据库的人不多!

subset(sample.data, sapply(files, function(df) any(df$extension == ".R")))