[英]R : How to resolve invalid subscript type 'list'
Origianl dataframe is 原始数据框为
id status name ...
1 0 V
1 0 S
1 1 V
1 0 V
2 0 V
2 1 V
2 1 S
3 0 V
3 1 S
4 1 S
4 0 V
4 1 V
Out of this, I expect to fetch id 2 and 3 as in their subset I have a "0-V" combination followed by "1-S" 因此,我希望获取id 2和3,因为在它们的子集中,我有一个“ 0-V”组合,后跟“ 1-S”
I have split that into a list of dataframes and I want to fetch the id's for some particular conditions on those dataframes but I get the error 我已将其拆分为数据帧列表,我想获取这些数据帧上某些特定条件的ID,但出现错误
invalid subscript type 'list'
The structure is like follows 结构如下
"1"
id status name ...
1 0 V
1 0 S
1 1 V
1 0 V
"2"
id status name ...
2 0 V
2 1 V
2 1 S
...
Now I need to find a condition such that I get the id where there is a record of 0 status and name 'V' followed by a 1 status and name "S" like for id 2. 现在,我需要找到一个条件,以便获得ID,其中记录有0个状态和名称“ V”的记录,然后是1个状态和名称“ S”,如ID 2。
For doing so, I'm trying to write a function for sapply but I'm not able to subscript the data as I'm assuming it to be 为此,我正在尝试为sapply写一个函数,但是由于我假设它是
Here is the code 这是代码
q4 <- result1[,c("id", "name", "date", "status")]
lstQ <- split(q4, result1$id)
f3 <- function(g) {
g[g$status == 0 & g$name == "V",]
}
e <- as.data.frame(names(lstQ)[sapply(lstQ, f3)])
How can I subscript my dataframe to get the desired check condition? 如何下标数据框以获得所需的检查条件?
Your data: 您的数据:
DF <- read.table(text = "id status name
1 0 V
1 0 S
1 1 V
1 0 V
2 0 V
2 1 V
2 1 S
3 0 V
3 1 S
4 1 S
4 0 V
4 1 V", header = TRUE)
Let's write a function: 让我们编写一个函数:
fun <- function(DF) {
one <- DF$status == 0 & DF$name == "V"
two <- DF$status == 1 & DF$name == "S"
#Is two TRUE after one is TRUE?
if (any(one) && any(two)) return(any(which(two) > which.max(one)))
return(FALSE)
}
Use a "group by" type function. 使用“分组依据”类型的函数。 I like data.table, others prefer dplyr: 我喜欢data.table,其他人则喜欢dplyr:
library(data.table)
setDT(DF)
DF[, fun(.SD), by = id]
# id V1
#1: 1 FALSE
#2: 2 TRUE
#3: 3 TRUE
#4: 4 FALSE
You can then use setDF
if you prefer or keep working with data.table and its syntax. 然后,您可以根据需要使用setDF
或继续使用data.table及其语法。 Subsetting could be the trivial next step. 子集化可能是微不足道的下一步。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.