Home > Software design >  Set character column of dataframe to missing if length<n
Set character column of dataframe to missing if length<n

Time:11-08

Very new to R here.

I have a dataframe with a character column "col1":

col1 <- c("org","blorg","forg","chorg","horg","blorg","horg","phthorg")
col2 <- c("a","b","c","d","a","b","e","f")

df<-data.frame(col1, col2)

I would like to set the values with fewer than 5 characters to missing so I end up with:

c(NA,"blorg",NA,"chorg",NA,"blorg",NA,"phthorg")

I have tried the following:

if(nchar(as.character(df$col1))<5) {df$col1<-NA}

but I get the error "the condition has length > 1".

CodePudding user response:

nchar is vectorized, so we can directly apply instead of if/else (which expects a single TRUE/FALSE as input) Or may use ifelse/replace/case_when which are vectorized, but it is not needed as we need to replace only to NA based on the condition

df$col1[nchar(df$col1) <5] <- NA

-output

> df$col1
[1] NA        "blorg"   NA        "chorg"   NA        "blorg"   NA        "phthorg"
  •  Tags:  
  • r
  • Related