使用R中的另一个列表子集数据框列中的列表

时间:2013-08-30 16:13:57

标签: r dataframe subset

我在R中有一个如下所示的数据框:

    id  event_explain
1   80  list("Minutes played", 0, 0)
2   81  list("Minutes played", 0, 0)
3   82  list("Bonus", 2, 2, "Clean sheets", 1, 4, "Minutes played", 90, 2)

我试图在“Minutes Played”之后提取数字,所以在这个例子中,我最终会得到类似0,0,90的内容。

我创建了某种索引列表来识别元素“Minutes Played”

    list(c(TRUE, FALSE, FALSE), c(TRUE, FALSE, FALSE), c(FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE)

并且想到也许我可以(以某种方式)置换每个列表元素中的T / F然后拉出元素后面的数字。

问题是,我甚至无法弄清楚如何将dataframe列子集化以提取列表中的元素,更不用说置换真则和谬误了!

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

这是一个解决方案。

首先,一些示例数据:

mydf <- data.frame(
  id = c(80, 81, 82), event = I(
    list(list("Minutes played", 0, 0),
         list("Minutes played", 0, 0), 
         list("Bonus", 2, 2, "Clean sheets", 1, 4, "Minutes played", 90, 2))))

使用grep标识字符串“播放的分钟数”。这将返回数字位置。你想要之后的值,所以我们在grep的输出中加1来得到你想要的数字。

unlist(sapply(mydf$event, function(x) x[grep("Minutes played", x)+1]))
# [1]  0  0 90

或者,使用match

unlist(sapply(mydf$event, function(x) x[match("Minutes played", x)+1]))
# [1]  0  0 90

或者,因为您说您已经创建了索引列表,所以您可以使用以下内容:

## Your index list
Index <- list(c(TRUE, FALSE, FALSE), 
              c(TRUE, FALSE, FALSE), 
              c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE))

## Extracting what you want
unlist(mydf$event)[which(unlist(Index))+1]
# [1] "0"  "0"  "90"

答案 1 :(得分:0)

## borrow the man above's data
mydf <- data.frame(
  id = c(80, 81, 82), event = I(
    list(list("Minutes played", 0, 0),
         list("Minutes played", 0, 0), 
         list("Bonus", 2, 2, "Clean sheets", 1, 4, "Minutes played", 90, 2))))
result<-c()
for (i in 1:3) {
    if("Minutes played" %in% mydf$event[[i]]) {
        result<-c(result,mydf$event[[i]][which("Minutes played" == mydf$event[[i]])+1] )
    }
}