简体   繁体   English

R函数返回分组变量的已更改数据

[英]R function that returns altered data for grouped variables

I have a simple example to explain my problem: lets say I use the mtcars dataframe. 我有一个简单的示例来说明我的问题:可以说我使用mtcars数据框。 And I would like to alter the data of mtcars$qsec by using the following function: 我想通过使用以下功能来更改mtcars$qsec的数据:

TT.test <- function(x)
{
  x<- ifelse(x > 20,x,NA)
  return(x)
}

The catch is I want to alter the mtcars$qsec using this function by grouping on mtcars$gear and mtcars$cyl . 我要抓住的是我想通过分组mtcars$gearmtcars$cyl使用此功能来更改mtcars$qsec So I would like to get a dataframe or matrix that gives me a dataframe back with the altered data. 因此,我想获得一个数据框或矩阵,该数据框或矩阵为我提供一个包含更改后数据的数据框。 It is fine if the dataframe only contains the three columns considered. 如果数据框仅包含所考虑的三列,那就很好。

Do you have any suggestions on how to deal with this problem? 您对如何处理此问题有任何建议吗?

If you want to mutate just that column, and group by some others, here is how you can do it with dplyr: 如果您只想更改该列并按其他列进行分组,则可以使用dplyr进行以下操作:

library(dplyr)
mtcars %>% group_by(gear, cyl) %>% mutate(qsec = TT.test(qsec))

Here is the resulting output: 这是结果输出:

Source: local data frame [32 x 11]
Groups: gear, cyl [8]

     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
   (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl)
1   21.0     6 160.0   110  3.90 2.620    NA     0     1     4     4
2   21.0     6 160.0   110  3.90 2.875    NA     0     1     4     4
3   22.8     4 108.0    93  3.85 2.320    NA     1     1     4     1
4   21.4     6 258.0   110  3.08 3.215    NA     1     0     3     1
5   18.7     8 360.0   175  3.15 3.440    NA     0     0     3     2
6   18.1     6 225.0   105  2.76 3.460 20.22     1     0     3     1
7   14.3     8 360.0   245  3.21 3.570    NA     0     0     3     4
8   24.4     4 146.7    62  3.69 3.190    NA     1     0     4     2
9   22.8     4 140.8    95  3.92 3.150 22.90     1     0     4     2
10  19.2     6 167.6   123  3.92 3.440    NA     1     0     4     4
..   ...   ...   ...   ...   ...   ...   ...   ...   ...   ...   ...

If you want to mutate all columns, you can do something like this: 如果要突变所有列,可以执行以下操作:

mtcars %>% group_by(gear, cyl) %>% mutate_each(funs(TT.test))

OR if you want to mutate some select columns (multiple), you can do something like this: 或者,如果您要更改某些选择列(多个),则可以执行以下操作:

mtcars %>% group_by(gear, cyl) %>% mutate_each(funs(TT.test), c(qsec, hp))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM