I have a simple function:
new_function <- function(x)
{
letters <- c("A","B","C")
new_letters<- c("D","E","F")
if (x %in% letters) {"Correct"}
else if (x %in% new_letters) {"Also Correct"}
else {x}
}
I make a dataframe
with letters:
df <- as.data.frame(LETTERS[seq( from = 1, to = 10 )])
names(df)<- c("Letters")
I want to apply the function on the dataframe
:
df$result <- new_function(df$Letters)
And it doesn't work (the function only writes "Correct")
I get this warning:
Warning message: In if (x %in% letters) { : the condition has length > 1 and only the first element will be used
ifelse
instead ofif/else
– akrunif (x %in% letters) {"Correct"}
, this will return a vector ofTRUE
andFALSE
value. One for each letter. – P1storius