Home > database >  How do I create two new variables out of one variable, and attach dummy values to it in R?
How do I create two new variables out of one variable, and attach dummy values to it in R?

Time:02-05

I am completely new to any kind of coding, nevermind R in particular, so my days of googling have not been very helpful. I would really appreciate any kind of help/insights!

I would like to know how to get two new variables out of the original variable, and attach new values to it - basically I start with this:

starting point

and want to obtain this:

desired result

I managed to get it in long format with melt(dataname, id.vars=c("ID")) and the ID & value I get are good. But there is only one variable with my four headers (loudHot, quietHot, loudCold, quietCold) repeated - how do I create two new variables out of this and assign the values to it (e.g. that "Volume" has the value 1 when the original variable is loudHot or loudCold and 0 if its quietHot or quietCold, and then "Temp" is 1 when the original variable is loudHot or quietHot and 0 when its loudCold or quietCold)?

CodePudding user response:

I wouldn't be too hard on yourself - this isn't really trivial. Anyway, you can use pivot_longer from tidyr and some data manipulation with dplyr to achieve your desired outcome:

library(dplyr)
library(tidyr)

df %>%
  pivot_longer(-ID) %>%
  mutate(Volume = as.numeric(grepl("loud", name)),
         Temp   = as.numeric(grepl("Hot",  name))) %>%
  select(ID, Volume, Temp, value)
#> # A tibble: 32 x 4
#>       ID Volume  Temp value
#>    <dbl>  <dbl> <dbl> <dbl>
#>  1     2      1     1    14
#>  2     2      0     1    16
#>  3     2      1     0    16
#>  4     2      0     0    15
#>  5     4      1     1    19
#>  6     4      0     1    15
#>  7     4      1     0    10
#>  8     4      0     0     8
#>  9     6      1     1    11
#> 10     6      0     1    17
#> # ... with 22 more rows

Data

df <- data.frame(ID        = (1:8) * 2,
                 loudHot   = c(14, 19, 11, 20, 18, 17, 16, 2),
                 quietHot  = c(16, 15, 17, 5, 10, 10, 15, 0),
                 loudCold  = c(16, 10, 10, 4, 3, 2, 14, 2),
                 quietCold = c(15, 8, 17, 8 ,10, 12, 5, 0))

As a tip for any future SO questions, please don't post images of data. Folks here need to be able to cut and paste the text of your data to test and verify solutions. Ideally, you should do this by the output of the dput function into a code block. People rarely go to the effort of manually transcribing data from your images.

Created on 2022-02-04 by the Result of R Code

  •  Tags:  
  • Related