简体   繁体   English

从包含 R 中每行列表中的列表的数据帧的每一行中提取一个参数

[英]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.

相关问题 数据框列表-从每个数据框提取一行 - List of dataframes - extracting a row from each dataframe R 将 dataframe 转换为每行每列的唯一成员列表 - R convert dataframe to list of unique memberships per column for each row 将 dataframe 转换为每行列表,在 R 中每行创建一个向量 - Turn dataframe into list per row creating a vector per row in R r从数据框中提取列表元素 - r extracting list elements from within dataframe 将数据帧的每一行转换为R中的新列表 - Convert each row of dataframe to new list in R 将包含列表的数据框列中的每个单元格转换为数据框中的一行 - Converting each cell in a dataframe column, containing a list, into a row in the dataframe R:如何将包含“列表”的行扩展为多行...每个列表成员一个? - R: how to expand a row containing a "list" to several rows...one for each list member? R:如何将列表附加到列表,然后将每个列表分配给数据框中的一行? - R: How to append a list to a list, and then assign each list to a row in a dataframe? 如何从一个列表中创建一个 dataframe,该列表的元素是包含一个 dataframe 的列表,每个 R - How to create one dataframe from a list whose elements are lists containing one dataframe each in R 向列表的每个数据框中添加一行,其中包含某些行的平均值 - Add a row to each dataframe of the list containing mean values of certain rows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM