Could also try using length<-
length<-
df[] <- t(apply(df, 1, function(x) `length<-`(na.omit(x), length(x)))) df # X1 X2 X3 X4 # x1 a b c <NA> # x2 d e <NA> <NA> # x3 f g h i # x4 j k l <NA>
2.1m questions
2.1m answers
60 comments
57.0k users