循环遍历R中的多个数据帧

时间:2021-01-08 12:47:24

New to R here. I have multiple data frames (with the same variables) from which I want to create subsets from ( by keeping several identical variables).

这里是R的新手。我有多个数据框(具有相同的变量),我想从中创建子集(通过保留几个相同的变量)。

I saved the name of the data frames by:

我通过以下方式保存了数据框的名称:

dfs<-Filter(function(x) is.data.frame(get(x)) , ls())

I thought I could create subsets by the below:

我以为我可以通过以下方式创建子集:

for(d in dfs) {assign(paste0(d,"_subset"), subset(d, select = c(x, y, z)))}

But it doesn't look like the "d" in the subset function is recognized as a dataset. Can anyone help?

但它看起来不像子集函数中的“d”被识别为数据集。有人可以帮忙吗?

1 个解决方案

#1


0  

To elaborate on Ben's comment:

详细阐述Ben的评论:

You typically know the names of whats in your environment. Using ls() and get() will get you that list of data frames in your environment, provided they are the only thing there. So if there's an iris or mtcars lying about, it'll grab that too.

您通常知道环境中的最新名称。使用ls()和get()将获得环境中的数据框列表,前提是它们是唯一的。因此,如果有一个虹膜或mtcars说谎,它也会抓住它。

So you're better off on creating a explicit list of the data frames that you created.

因此,您最好创建一个您创建的数据框的显式列表。

Once you have that list, you can use a loop, but the more R way of doing things would be using the apply family of functions, which iterates a function on elements of a list or vector.

一旦你有了这个列表,就可以使用一个循环,但更多的R做事方式是使用apply系列函数,它在列表或向量的元素上迭代一个函数。

In this case you would use lapply and an anonymous function so subset each element of your list of data frames. This will return your list of subsetted data frames.

在这种情况下,您将使用lapply和匿名函数,以便对数据框列表中的每个元素进行子集化。这将返回子集化数据帧列表。

list <- list(mtcars, mtcars, mtcars)

lapply(list, function(x) subset(x, select = c(mpg, cyl)))

#1


0  

To elaborate on Ben's comment:

详细阐述Ben的评论:

You typically know the names of whats in your environment. Using ls() and get() will get you that list of data frames in your environment, provided they are the only thing there. So if there's an iris or mtcars lying about, it'll grab that too.

您通常知道环境中的最新名称。使用ls()和get()将获得环境中的数据框列表,前提是它们是唯一的。因此,如果有一个虹膜或mtcars说谎,它也会抓住它。

So you're better off on creating a explicit list of the data frames that you created.

因此,您最好创建一个您创建的数据框的显式列表。

Once you have that list, you can use a loop, but the more R way of doing things would be using the apply family of functions, which iterates a function on elements of a list or vector.

一旦你有了这个列表,就可以使用一个循环,但更多的R做事方式是使用apply系列函数,它在列表或向量的元素上迭代一个函数。

In this case you would use lapply and an anonymous function so subset each element of your list of data frames. This will return your list of subsetted data frames.

在这种情况下,您将使用lapply和匿名函数,以便对数据框列表中的每个元素进行子集化。这将返回子集化数据帧列表。

list <- list(mtcars, mtcars, mtcars)

lapply(list, function(x) subset(x, select = c(mpg, cyl)))