Ale
Ale

Reputation: 55

Add new column to dataframe in R using dplyr

Thanks to inputs found in StackOverflow, I figured out how to add a column to an existing data frame based on conditions.

When I run this code chunk

testmain %>% 
  mutate(wealth = case_when(.$savings_1 == "0" & .$savings_5 == "0" ~ "No savings",
                            .$savings_1 == "1" & .$savings_5 == "0" ~ "1,000 Sh savings",
                            .$savings_1 == "1" & .$savings_5 == "1" ~ "5,000 Sh savings",
                            .$savings_1 == "0" & .$savings_5 == "1" ~ "Special case"))

the 'result' is shown in a preview window (I am writing a Markdown file in R). This result contains my newly added variable wealth: See last column

When I subsequently run the code View(testmain) to look at the dataframe, the new variable isn't there. Can anyone give me a hint on what I'm doing wrong?

Upvotes: 0

Views: 1034

Answers (1)

Fleur De Lys
Fleur De Lys

Reputation: 480

You need to assign the result to something.

For example,

testmain2 <- testmain %>%
  mutate(wealth = case_when(.$savings_1 == "0" & .$savings_5 == "0" ~ "No savings",
                            .$savings_1 == "1" & .$savings_5 == "0" ~ "1,000 Sh savings",
                            .$savings_1 == "1" & .$savings_5 == "1" ~ "5,000 Sh savings",
                            .$savings_1 == "0" & .$savings_5 == "1" ~ "Special case"))

Then View(testmain2) should show the added column.

Side note: It's recommended to have a default value in case_when. You can do this by adding

case_when(
  x == y   ~ z,
  TRUE     ~ somedefaultvalue
)

Upvotes: 1

Related Questions