Home > database >  For loop skipping columns in R
For loop skipping columns in R

Time:12-17

I want to concatenate text across 20 columns of my dataset (dat), skipping all NA values.

For example, if the first row had "cat" in column 1, "dog" in column 2, and NA in column 3, I want to compile that as "cat dog" in a new column (dat$results). Here's what I have:

m <- ""

for(i in 1:20){
  if(!is.na(dat[,i])){
    m <- paste(m, dat[,i], sep = " ") 
  }
  else {
  next 
  }
}

dat$results <- m 

The loop only runs up to column 3 (which is NA for my first row). Not a problem for that first row, BUT other rows that do have text in column 3 don't get that column compiled. What can I do here?

CodePudding user response:

Maybe just concatenate all the columns in one go and remove na's and double spaces afterward

dat$result <- gsub('\\s\\s', ' ', gsub('NA', '', do.call(paste, d[,1:20])))
  • Related