I am working with the below df:
first_column<-c(1,2,3,4)
second_column<-c(1,2,"NA",4)
df<-data.frame(first_column,second_column)
df$test=ifelse(df$first_column==df$second_column,0,1)
> df
first_column second_column test
1 1 1 0
2 2 2 0
3 3 NA 1
4 4 4 0
What I would like to do are 2 things, 1) to remove an entire row if there is NA in second column, how should I do with & without dplyr? 2) If I would like to have an result returning to the first column if the test column shows non-zero, that is, in this case, returning to first column # 3 based on "1" on test column. May I know how should I tackle these 2 things? Many thanks for your help.
CodePudding user response:
You can use tidyr::drop_na
:
library(tidyr)
df %>%
drop_na(second_column)
And complete.cases
in base R:
df[complete.cases(df$second_column), ]
Note that in your data frame, NA is character, "NA"
. It should be just NA
.
CodePudding user response:
We could use base R
df[!is.na(df$second_column),]