[英]Extracting one parameter from each row of a dataframe containing a list within a list per row in R
I have a dataframe with one column where each row contains a list, which in itself contains a list.我有一个包含一列的数据框,其中每一行包含一个列表,该列表本身包含一个列表。
This looks something like this per row: list(statistic = c(x = 10.69), parameter = c(df = 4.63), p.value = c(P = 0.05))每行看起来像这样: list(statistic = c(x = 10.69), parameter = c(df = 4.63), p.value = c(P = 0.05))
To explain the structure of df somehow more:以某种方式解释 df 的结构:
row1<-list(statistic = c(x = 10.69), parameter = c(df = 4.63), p.value = c(P = 0.05))
row2<-list(statistic = c(x = 10.69), parameter = c(df = 4.63), p.value = c(P = 2.5))
...
row300<-row1<-list(statistic = c(x = 10.69), parameter = c(df = 4.63), p.value = c(P = 4.6))
df<-rbind(row1, row2, ... , row300) #this is the dataframe I have
The only thing of interest for me is the p-value which is why I would like to extract this somehow so that ideally, I want to create a dataframe that has the same amount of rows as the original dataframe and each row contains the p.value, meaning what's in the list for p.value so " P = 0.05 " in this case.我唯一感兴趣的是 p 值,这就是为什么我想以某种方式提取它,以便理想情况下,我想创建一个与原始数据帧具有相同行数且每行包含 p 的数据帧。值,表示 p.value 列表中的内容,因此在本例中为“ P = 0.05 ”。
So to come back to the above code example: what I need is a new dataframe that contains the extracted p.value in each row so that the output of calling a row would be所以回到上面的代码示例:我需要的是一个新的数据帧,其中包含每行中提取的 p.value,以便调用行的输出是
df2[1] 0.05 df2[2] 2.5 ... df2[300] 4.6
Extracting the value I need for one row works fine like this: df[[1]][[1]][['p.value']]
提取一行所需的值工作正常:
df[[1]][[1]][['p.value']]
However when I want to do this for each of the 300 rows of the dataframe it does not work.但是,当我想对数据框的 300 行中的每一行执行此操作时,它不起作用。 I was thinking about a for loop where I extract each p.value per row and subset them in a new dataframe:
我正在考虑一个 for 循环,我提取每行的每个 p.value 并将它们子集在一个新的数据帧中:
x<-1:300
for(i in x){
one<-data.frame
one<-rbind(df[[1]][[i]][['p.value']])
}
尝试像这样使用sapply
-
p_values <- sapply(df, function(x) x[[1]][['p.value']])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.