简体   繁体   English

根据其他列的分组在R数据框中创建新列

[英]Create new column in R data frame based on grouping of other column

Using mtcars from the datasets package, I want to added an addition column that is created from mtcars$mpg , such that: 使用datasets包中的mtcars ,我想添加一个从mtcars$mpg创建的附加列,例如:

If mtcars$mpg >=15 and <19 then "Good"
If mtcars$mpg >=19 and <27 then "Average"
If mtcars$mpg >=27 then "Bad"

You can use ifelse() 您可以使用ifelse()

mtcars$mpg2 <- ifelse(mtcars$mpg >=15 & mtcars$mpg < 19,
                      "Good",
                      ifelse(mtcars$mpg >=19 & mtcars$mpg < 27,
                             "Average",
                             ifelse(mtcars$mpg > 27, "Bad", "")))

If you want the tidyverse solution, try this: 如果您需要tidyverse解决方案,请尝试以下操作:

library(tidyverse)

df <- as_tibble(mtcars)

df %>% 
  mutate(mpg_quality = case_when(mpg >= 15 & mpg < 19 ~ "good",
                                 mpg >= 19 & mpg < 27 ~ "average",
                                 mpg >= 27 ~ "bad"))

Which gives us: 这给了我们:

# A tibble: 32 x 12
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb mpg_quality
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>       <chr>
 1  21.0     6 160.0   110  3.90 2.620 16.46     0     1     4     4     average
 2  21.0     6 160.0   110  3.90 2.875 17.02     0     1     4     4     average
 3  22.8     4 108.0    93  3.85 2.320 18.61     1     1     4     1     average
 4  21.4     6 258.0   110  3.08 3.215 19.44     1     0     3     1     average
 5  18.7     8 360.0   175  3.15 3.440 17.02     0     0     3     2        good
 6  18.1     6 225.0   105  2.76 3.460 20.22     1     0     3     1        good
 7  14.3     8 360.0   245  3.21 3.570 15.84     0     0     3     4        <NA>
 8  24.4     4 146.7    62  3.69 3.190 20.00     1     0     4     2     average
 9  22.8     4 140.8    95  3.92 3.150 22.90     1     0     4     2     average
10  19.2     6 167.6   123  3.92 3.440 18.30     1     0     4     4     average
# ... with 22 more rows

The df <- as_tibble(mtcars) isn't necessary but I like tibbles so I included it. df <- as_tibble(mtcars)不是必需的,但我喜欢小标题,因此将其包括在内。

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

相关问题 以 R 数据框中的其他列为条件创建新的二进制列 - Create new binary column conditional on other column in R data frame 根据 R 中的条件列名称创建新的数据框列 - Create new data frame column based on conditioned column names in R 根据 R 中数据框中的其他列创建新列 - Creating new column based on other columns in data frame in R 根据 R 中数据框中所有其他列中的字符串值,使用 dplyr 创建一个新列 - Create a new column using dplyr based on string values in all other columns in a data frame in R 如何根据 R 中数据框中其他一些列的模式创建新列 - How to create a new column based on the mode of some other columns in a data frame in R 根据R中的其他行和列组合在数据框中创建行 - Create rows in a data frame based on other rows and column combination in R 根据另外两列的值创建一个新的数据框列 - Create a new data frame column based on the values of two other columns R-根据条件在数据框中创建新列 - R - create new column in data frame based on conditional 在 R 数据框中创建基于逻辑的列 - Create a logic based column in R data frame 如何基于R中数据框中的其他列为列中的每个元素获取新名称? - How to get a new name for each element in a column based on other column in a data frame in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM