-1

I'm tring to assign dataframe value to a dataframe in ifelse query,but failed.I have no idea about that

df <- data.frame(x=c('a','g','s'),y=c(2,3,4))
df_1 <- ifelse(2>1,df,NULL)

execute and get the following result:

> df_1
[[1]]
[1] a g s
Error in if (n <= 1L || lenl[n] <= width) n else max(1L, which.max(lenl >  : 
  missing value where TRUE/FALSE needed

the actual results should be

> df
  x y
1 a 2
2 g 3
3 s 4

Cui Weijie
  • 11
  • 2

1 Answers1

0

The ifelse function works with scalars and vectors, but maybe not with data frames. Try using a regular if-else statement:

if (2 > 1) {
    df_1 <- df
}
else {
    df_1 <- NULL
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360