Home > Software design >  How do add single datapoints after a chart review in R
How do add single datapoints after a chart review in R

Time:12-07

I am trying to add my new data collected from a chart review into my database, but cant seem to get there.

I've tried programming a new variable and then using a logic function to pick which datapoint to use, but using a whole variable is not working, as it is a vector and not a single datapoint. How can I programm it to use the datapoint from the same observation but a different variable?

mydf <- structure(list(Year = c(2014L, 2014L, 2015L, 2015L, 2016L),
                       Var1 = c(15, 12, 2, 16, NA), 
                       Var2 = c(15, 13, 104, NA, NA), 
                       Agree = c(1, 0, 0, 1, 1)), 
                  .Names = c("Year",  "Var1", "Var2", "Agree"), 
                  row.names = c(NA, 5L), class = "data.frame")

## mydf

#    Year Var1 Var2 Agree
#  1 2014   15   15  1
#  2 2014   13   12  0
#  3 2015    2  104  0
#  4 2015   16   NA  1
#  5 2016   NA   NA  1


mydf$true <- if_else(mydf$Agree == 1, mydf$Var1, mydf$Var2) 

## Error Message
# Error: `true` must be length 0 (length of `condition`) or one, not 5.
# Run `rlang::last_error()` to see where the error occurred.

## What I want
#   Year Var1 Var2 Agree True
# 1 2014   15   15     1   15
# 2 2014   13   12     0   12
# 3 2015    2  104     0  104
# 4 2015   16   NA     1   16
# 5 2016   NA   NA     1   NA

Any ideas?

CodePudding user response:

As @Maël pointed out, you probably loaded multiple packages with the if_else function. To fix this error you can specify the package you want to use.

library(dplyr)

mydf$true <- dplyr::if_else(mydf$Agree == 1, mydf$Var1, mydf$Var2) 

A small addition, which will be helpful for you in the long run in working with R, I recommend you to deal with dplyr and mutate. Check the code below as an example for your issue above:

mydf %>%
  mutate(true = ifelse(mydf$Agree == 1, mydf$Var1, mydf$Var2))

CodePudding user response:

I'm not sure which packages you have initially. I did it this way and got the answer you needed: '''

mydf <- structure(list(Year = c(2014L, 2014L, 2015L, 2015L, 2016L),
                       Var1 = c(15, 12, 2, 16, NA), 
                       Var2 = c(15, 13, 104, NA, NA), 
                       Agree = c(1, 0, 0, 1, 1)), 
                  .Names = c("Year",  "Var1", "Var2", "Agree"), 
                  row.names = c(NA, 5L), class = "data.frame")

library(dplyr)
mydf$true <- if_else(mydf$Agree == 1, mydf$Var1, mydf$Var2) 

'''

  • Related