简体   繁体   中英

Subsetting a list in a dataframe column with another list in R

I have a dataframe in R that looks like this:

    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)

I'm trying to pull out the number after "Minutes Played", so in this example, I would end up with something like 0,0,90.

I have created some kind of index list to identify the element "Minutes Played"

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

and was thinking that perhaps I could permute (somehow) the T/F in each list element to then pull out the number following the element.

The problem is, I can't even work out how to subset the dataframe column to pull out an element of a list, let alone permute the Trues and Falses!

Any ideas?

Here's one solution.

First, some sample 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))))

Use grep to identify the string "Minutes played". That will return the numeric position. You want the value just after that, so we add 1 to the output of grep to get the numbers you're looking for.

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

Or, alternatively, with match :

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

Alternatively, since you say you have already created an index list, you can just use the following:

## 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"
## 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] )
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM