Home > OS >  Is there any way to keep the total number of rows which contain a text?
Is there any way to keep the total number of rows which contain a text?

Time:10-13

In a data frame we have this format:

df <- data.frame(id = c(1,2,3), text_en_1 = c("Google", "Yahoo", "Amazon"), text_en_2 = c("Amazon", "Yahoo", "NA"), text_en_3 = c("Ieekkj","NA","NA"))

NA in this data frame is a missing value not a text. I just insert it to show the missing value.

How is it possible to could for every row using the id column how many columns have a value.

Example expected out put

data.frame(id = c(1,2,3), number_of_columns_with_text = c(3,2,1))

CodePudding user response:

data.frame(id = df[,1], number_of_columns_with_text = rowSums(!is.na(df[,-1])))
  •  Tags:  
  • r
  • Related