使用两列数据拆分数据框,并对结果数据框列表应用通用转换 [英] Split dataframe using two columns of data and apply common transformation on list of resulting dataframes

查看:84
本文介绍了使用两列数据拆分数据框,并对结果数据框列表应用通用转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据两列中的值将一个大型数据框拆分为一个数据框列表。然后,我想在结果列表中的所有数据帧上应用通用数据转换(滞后转换)。我知道split命令,但一次只能使它处理一列数据。

I want to split a large dataframe into a list of dataframes according to the values in two columns. I then want to apply a common data transformation on all dataframes (lag transformation) in the resulting list. I'm aware of the split command but can only get it to work on one column of data at a time.

推荐答案

您需要将所有要分割的因素放在一个列表中,例如:

You need to put all the factors you want to split by in a list, eg:

split(mtcars,list(mtcars$cyl,mtcars$gear))

然后您可以使用 lapply

Then you can use lapply on this to do what else you want to do.

如果要避免结果中的行数据框为零,则有一个 drop 参数,其默认值与 [函数中的drop参数相反。

If you want to avoid having zero row dataframes in the results, there is a drop parameter whose default is the opposite of the drop parameter in the "[" function.

split(mtcars,list(mtcars$cyl,mtcars$gear), drop=TRUE)

这篇关于使用两列数据拆分数据框,并对结果数据框列表应用通用转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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