Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

replace - Replacing values in R dataframes based on conditional

I'm having trouble replacing values in a column of a R dataframe based upon conditions related to other data variables.

I've created a new dataframe called VAED1 based on the left join between the original data frame VAED (has over 20 variables) and another dataframe called new_map (has only 3 variables and one is called Category)

Here is the code i wrote that works fine:

#join the left side table (VAED) with the right side table (new_map) with the left join function
VAED1 <- VAED %>%
left_join(new_map, by = c("ID1" = "ID2"), suffix= c("_VAED", "_MAP"))***

I then added a three extra columns (nnate, NICU, enone) to the dataframe VAED1 using mutate function to create a new dataframe VAED2:

VAED2 <- VAED1 %>%
mutate(nnate = if_else((substr(W25VIC,1,1) == "P") & (CARE != "U") & (AGE < 1) , "Y", "N"))%>%
mutate(NICU = if_else((nnate == "Y") & (ICUH > 0), "Y", "N"))%>%
mutate(enone = if_else((EMNL == "E") , "Emerg", "Non-emerg")%>%***

Everything works fine to this point.

Finally I wanted to replace the values in one column called Category (this was a character variable in the original joined dataset new_map) based upon certain conditions of other variables in the dataframe. So only change values in the Category column when W25VIC and CARE variables equal certain values. Otherwise leave the original value,)

Use the code:

Category <- if_else((W25VIC == "R03A") & (SAMEDAY == "Y"), "08 Other multiday", Category)

This always shows an error - object 'W25VIC' and 'SAMEDAY' not found. It seems straightforward but the last line of code doesn't work no matter what i do. I check the dataframe using a Head command to make sure the data columns are there during each step. They exist but the code doesn't seem to recognise them. Grateful for any ideas on what I am doing wrong. Also used the command Category[(W25VIC == "R03A") & (SAMEDAY == "Y")] <- "08 Other multiday" Still same error message.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think it is worth to readup on how the magrittr pipe works. The pipe takes an object from the left-hand side of an expression and moves it as the first argument into a function on the right.

So x %>% f() becomes f(x) and x %>% f(y) becomes f(x, y). In your last statement

Category <- if_else((W25VIC == "R03A") & (SAMEDAY == "Y"), "08 Other multiday", Category)

the x and the function of what to do following the evaluation of the if_else statement is missing. Here is an example how to use the pipe operator together with an if_else statement to generate a new column:

library(tidyverse)
data <- mtcars
new_data <- data %>% mutate( evaluation = if_else(hp > 150, "awesome", "lame"))
head(new_data, 20)
#>     mpg cyl  disp  hp drat    wt  qsec vs am gear carb evaluation
#> 1  21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4       lame
#> 2  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4       lame
#> 3  22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1       lame
#> 4  21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1       lame
#> 5  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2    awesome
#> 6  18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1       lame
#> 7  14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4    awesome
#> 8  24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2       lame
#> 9  22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2       lame
#> 10 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4       lame
#> 11 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4       lame
#> 12 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3    awesome
#> 13 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3    awesome
#> 14 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3    awesome
#> 15 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4    awesome
#> 16 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4    awesome
#> 17 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4    awesome
#> 18 32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1       lame
#> 19 30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2       lame
#> 20 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1       lame

Created on 2021-01-07 by the reprex package (v0.3.0)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...