在R的data.frames列表中提取一组多个变量 [英] extracting one set of multiple variables in a list of data.frames in R

查看:287
本文介绍了在R的data.frames列表中提取一组多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个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中),该结构在每一轮中都从每个任意列中提取一组? 我想要的输出是一个列表,其中每个研究的ESLprofscope值和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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆