简体   繁体   English

如何根据R中因子的级别组合两个变量

[英]How to combine two variables according to levels of a factor in R

I am struggling with a question, which is probably really basic, but I am not able to find a solution. 我正在努力解决一个可能非常基本的问题,但我无法找到解决方案。 I would greatly appreciate any help. 我非常感谢任何帮助。

I have a dataframe containing two variables, which I would like to merge in the same variable. 我有一个包含两个变量的数据框,我想在同一个变量中合并。 The dataframe looks something like this: 数据框看起来像这样:

    id <- 1:6
    color <- c(rep("red", 3), "blue", "red", "blue")
    value2 <- 20:25
    value1 <- 25:30
    wanted_outcome <- c(25,26,27,23,29,25)
    data_sample <- data.frame(id, color, value1, value2, wanted_outcome)
    data_sample

      id color value1 value2     wanted_outcome
   1  1   red     25     20             25
   2  2   red     26     21             26
   3  3   red     27     22             27
   4  4  blue     28     23             23
   5  5   red     29     24             29
   6  6  blue     30     25             25

The outcome that I want is in the last column. 我想要的结果是在最后一栏。 Basically I would like to create a new variable, which contains the values from the variable value1 for red items and the values from value2 for blue items. 基本上我想创建一个新变量,它包含红色项的变量value1和蓝色项的value2的值。

This is what I am trying, however, it is not producing the desired result, as R is replacing the values starting from the first one and not row by row. 这是我正在尝试的,但是,它没有产生所需的结果,因为R正在替换从第一个开始而不是逐行的值。

   data_sample$value_combined[color=="red"] <- value1
   data_sample$value_combined[color=="blue"] <- value2

   data_sample

   id    color value1 value2     wanted_outcome value_combined
   1  1   red     25     20             25             25
   2  2   red     26     21             26             26
   3  3   red     27     22             27             27
   4  4  blue     28     23             23             20
   5  5   red     29     24             29             28
   6  6  blue     30     25             25             21

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks in advance. 提前致谢。

using ifelse (slow, but easy): 使用ifelse (慢,但很容易):

data_sample <- transform(data_sample, 
                         wanted = ifelse(color == "red", 
                                         value1, 
                                         ifelse(color == "blue", 
                                                value2, 
                                                NA)))

or 要么

data_sample <- transform(data_sample, 
                         wanted = ifelse(color == "red", 
                                         value1, 
                                         value2))

if there are only those two colors. 如果只有那两种颜色。

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

相关问题 如何在 R 中进行 FOR 和 IF 循环以根据两个因子级别对值求和并创建新变量? - How to FOR and IF loop in R to sum values according to two factor levels and create new variable? 循环 R 中的因子级别 - 如何操作两个连续级别 - Looping over factor levels in R - how to operate two consecutive levels 将两个列表合并成两个级别的因子 - Combine two lists to make a factor of two levels R:根据因子水平的图例颜色 - R: Legend color according to factor levels 取两个水平的因子变量之差,同时保留R中的其他因子变量 - Take difference between two levels of factor variable while retaining other factor variables in R 什么是 R 纵向的最佳 model 代码:19 个时间点,6 组(1 个因素,2 个水平,1 个因素,3 个水平),两个响应变量 - What is the best model code for R longitudinal: 19 time points with 6 groups (1 factor with 2 levels, 1 factor with 3 levels), two response variables 如何在R中的两列之间折叠/合并所选因子水平 - How to collapse/join selected factor levels across two columns in R 如何根据因子水平重复相同的值? - How to repeat the same value according to factor levels? R Shiny:创建因子变量并定义水平 - R Shiny: Creating factor variables and defining levels R 将因子变量的级别堆栈到数据框中 - R stack levels of factor variables into a data frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM