Count NAs per row in dataframe [duplicate]
You can count the NAs in each row with this command: rowSums(is.na(dat)) where dat is the name of your data frame.
You can count the NAs in each row with this command: rowSums(is.na(dat)) where dat is the name of your data frame.
Use is.na DF <- data.frame(x = c(1, 2, 3), y = c(0, 10, NA), z=c(NA, 33, 22)) DF[!is.na(DF$y),]
You could try na.omit() to omit the values, then paste. Also, you could use toString(), as it is the equivalent of paste(…, collapse = “, “). apply(dd2, 1, function(x) toString(na.omit(x))) # [1] “A, AK2, PPT” “B, HFM1, PPT” “C, TRR” # [4] “D, TRR, RTT, GGT” “E, RTT” If you have specific columns you are … Read more
Wrap the condition in which: df[which(df$number1 < df$number2), ] How it works: It returns the row numbers where the condition matches (where the condition is TRUE) and subsets the data frame on those rows accordingly. Say that: which(df$number1 < df$number2) returns row numbers 1, 2, 3, 4 and 5. As such, writing: df[which(df$number1 < df$number2), … Read more
You may try pmax df$c <- pmax(df$a, df$b) df # a b c # 1 dog <NA> dog # 2 mouse <NA> mouse # 3 <NA> cat cat # 4 bird <NA> bird …or ifelse: df$c <- ifelse(is.na(df$a), df$b, df$a) For more general solutions in cases with more than two columns, you find several ways … Read more
Use rowSums. To remove rows from a data frame (df) that contain precisely n NA values: df <- df[rowSums(is.na(df)) != n, ] or to remove rows that contain n or more NA values: df <- df[rowSums(is.na(df)) < n, ] in both cases of course replacing n with the number that’s required
It didn’t work because status was a factor. When you mix factor with numeric then numeric is the least restrictive. By forcing status to be character you get the results you’re after and the column is now a character vector: TEST$UNIT[is.na(TEST$UNIT)] <- as.character(TEST$STATUS[is.na(TEST$UNIT)]) ## UNIT STATUS TERMINATED START STOP ## 1 ACTIVE ACTIVE 1999-07-06 2007-04-23 … Read more
You can do: x[, 1:2][is.na(x[, 1:2])] <- 0 or better (IMHO), use the variable names: x[c(“a”, “b”)][is.na(x[c(“a”, “b”)])] <- 0 In both cases, 1:2 or c(“a”, “b”) can be replaced by a pre-defined vector.
Here’s a data table approach that uses na.omit() across the columns, grouped by ID. library(data.table) setDT(df)[, lapply(.SD, na.omit), by = ID] # ID Col1 Col2 Col3 Col4 # 1: 1 5 10 15 20 # 2: 2 25 30 35 40
For the purpose of a “true-NA”: Seems the most direct route is just to modify the value returned by paste2 to be NA when the value is “” paste3 <- function(…,sep=”, “) { L <- list(…) L <- lapply(L,function(x) {x[is.na(x)] <- “”; x}) ret <-gsub(paste0(“(^”,sep,”|”,sep,”$)”),””, gsub(paste0(sep,sep),sep, do.call(paste,c(L,list(sep=sep))))) is.na(ret) <- ret==”” ret } val<- paste3(c(“a”,”b”, “c”, … Read more