Home > Software design >  R use an if/else statement to make a new column populated with conditional values
R use an if/else statement to make a new column populated with conditional values

Time:10-18

Our dataset:

mydat <- data.frame(
  Date=c("Jan 1, 2022", "Jan 1, 2022", "Jan 2, 2022","Jan 4, 2022", "Jan 4, 2022", "Jan 6, 2022"),
  City=c("Worcester","Boston","Ashburn","Brockton","Cambridge", "Springfield"),
  Users=c(3,34,1,2,1,2))

Which looks like this:

Date City Users
Jan 1, 2022 Worcester 3
Jan 1, 2022 Boston 34
Jan 2, 2022 Ashburn 1
Jan 4, 2022 Brockton 2
Jan 4, 2022 Cambridge 1
Jan 6, 2022 Springfield 2

I want to make a new column that has the values for a certain city, and blank for all the ones not in that city.

I tried to do this with an if/else like this:

mydat$boston_users <- if (dat$City == "Boston") mydat$Users else " "

What ends up happening is I get blanks in the new column, which looks like this:

Date City Users boston_users
Jan 1, 2022 Worcester 3
Jan 1, 2022 Boston 34
Jan 2, 2022 Ashburn 1
Jan 4, 2022 Brockton 2
Jan 4, 2022 Cambridge 1
Jan 6, 2022 Springfield 2

So I say well my statement must be broken, so when I put in something absurd that I KNOW won't be there, I am able to get the values to populate.

mydat$hello_users <- if ("hello" == "hello") mydat$Users else " "

Date City Users hello_users boston_users
Jan 1, 2022 Worcester 3 3
Jan 1, 2022 Boston 34 34
Jan 2, 2022 Ashburn 1 1
Jan 4, 2022 Brockton 2 2
Jan 4, 2022 Cambridge 1 1
Jan 6, 2022 Springfield 2 2

I know this is really simple but I'm missing it

CodePudding user response:

I use NA instead of blank " " because otherwise it will change the data type to string. You can put a list of cities in c("your_city") i.e. c("Boston", "NY", "York)

mydat$boston_users <- ifelse(dat$City %in% c("<Your_city>"), mydat$Users, NA)

CodePudding user response:

I figured it out

dat$BostonUsers <- ifelse(dat$City == "Boston", dat$Users, " ")

This creates a new column with the values from the previous column and the rest appear as blank. My issue before was that I was trying to make a function rather than just the ifelse feasure in R

  • Related