Reputation: 87
I want to create a new column in R dependant on other columns in r. The format would be
new_column 0 1 0 0 1
I have three variables that the new column will be made from
comorbidity
date_of_comorbidity
DOB
I have tried using ifelse but I get an error (this is what I have tried)
newdata$newcolumn<-NA
ifelse(newdata$comorbidity1==10 &newdata$date_of_comorbidity>=DOB, newdata$newcolumn<-1,
ifelse(newdata$comorbidity1==10 &newdata$date_of_comorbidity<DOB, newdata$newcolumn<-0))
the error message is
Error in ifelse(newdata$comorbidity1==10 &newdata$date_of_comorbidity<DOB, :
argument "no" is missing
can this be fixed or is there a better way of solving my problem
Upvotes: 0
Views: 305
Reputation: 143
It would be helpful if you could expand on the criteria you want to use to create new_column
. This link has some good tips on how to get the best answer on SO.
dplyr::mutate
is an easy way to create new columns in a dataframe. case_when
is useful for defining your criteria.
Create the data:
library(tidyverse)
test_data <- test_data <- structure(list(comorbidity = c(0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L,
0L, 1L), date_of_comorbidity = c(93L, 72L, 81L, 52L, 26L, 30L,
80L, 45L, 97L, 96L), DOB = c(123L, 145L, 192L, 176L, 189L, 164L,
158L, 170L, 145L, 121L)), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
# A tibble: 10 x 3
comorbidity date_of_comorbidity DOB
<int> <int> <int>
0 93 123
0 72 145
1 81 192
1 52 176
1 26 189
1 30 164
1 80 158
1 45 170
0 97 145
1 96 121
Create new_column
:
test_data <- test_data %>%
mutate(new_column = case_when(comorbidity == 1 & date_of_comorbidity > 50 ~ TRUE,
comorbidity == 0 | date_of_comorbidity <= 50 ~ FALSE))
# A tibble: 10 x 4
comorbidity date_of_comorbidity DOB new_column
<int> <int> <int> <lgl>
1 1 99 115 TRUE
2 1 89 147 TRUE
3 1 55 188 TRUE
4 0 63 155 FALSE
5 1 38 179 FALSE
6 1 1 166 FALSE
7 1 17 139 FALSE
8 1 9 127 FALSE
9 1 96 181 TRUE
10 1 61 127 TRUE
Upvotes: 0
Reputation: 7592
newdata$newcolumn<- ifelse(newdata$comorbidity1==10 & newdata$date_of_comorbidity>=newdata$DOB, 1,
ifelse(newdata$comorbidity1==10 & newdata$date_of_comorbidity<newdata$DO,0,NA))
Upvotes: 1