在R的data.frames列表中提取一组多个变量 [英] extracting one set of multiple variables in a list of data.frames in R
问题描述
假设我有一个data.frame
,例如 这 .名为autoreg
的列之后的任何数据列都是用户定义的任意列.因此,我将不知道列的名称或值.例如,在 此 中"ESL" "prof" "scope" "type"
的列由用户定义.
Suppose I have a data.frame
like THIS. Any columns of data after the column named autoreg
are arbitrary columns defined by the user. So, I won't know the columns names or values. For example, in THIS data.frame
columns named: "ESL" "prof" "scope" "type"
are defined by the user.
问题:
我如何拥有一个循环结构(在BASE R中),该结构在每一轮中都从每个任意列中提取一组? 我想要的输出是一个列表,其中每个研究的ESL
值prof
值scope
值和type
值彼此相邻.
How can I have a looping structure (in BASE R) that at each round, extracts one set of each of these arbitrary columns? My desired output is a list within which the ESL
values prof
values scope
values and type
values from each study are put next to each other.
我尝试了两个嵌套的lapply
(请参见下文),它们提取这些任意列的所有集合的所有值,但是如何一次提取每个这些任意列的集合呢?
I have tried two nested lapply
(see below) which extracts all values for all sets of these arbitrary columns but how can I extract one set of each of these arbitrary columns at a time?
D <- read.csv("https://raw.githubusercontent.com/izeh/i/master/i.csv", h = T) ## data.frame
L <- split(D, D$study.name) ; L[[1]] <- NULL
arb.names <- c("ESL", "prof", "scope", "type") ## arbitrary column names
a <- lapply(1:length(arb.names), function(j) lapply(1:length(L), function(i) L[[i]][arb.names[j]]))
推荐答案
可能是我们需要grep
'arb.names'从'L'中提取列集
May be we need to grep
the 'arb.names' to extract the set of columns from the 'L'
lapply(arb.names, function(nm) lapply(L, function(l1) l1[grep(nm, names(l1))]))
如果我们要将list
中的不同名称分组为单个list
,请使用transpose
If we want to group the different names across the list
as a single list
, use transpose
library(purrr)
lapply(arb.names, function(nm) transpose(lapply(L, function(l1) l1[grep(nm, names(l1))])))
或使用base R
m1 <- simplify2array(lapply(arb.names, function(nm)
lapply(L, function(l1) l1[grep(nm, names(l1))])))
split(m1, col(m1))
这篇关于在R的data.frames列表中提取一组多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!