0

That's how my data looks like:

structure(list(`Name1` = c("Mark", 
                                                           NA, NA, NA, NA, NA), Name2 = c(NA, "Stefan", 
                                                                                                                   "Clara", NA, NA, NA), `Name3` = c(NA, NA, 
                                                                                                                                                                           NA, "Max", "Pete", "Gabe"), `Name4` = c("Titan", 
                                                                                                                                                                                                                                                     NA_character_, NA_character_, NA_character_, NA_character_, NA_character_
                                                                                                                                                                           ), `Name5` = c(NA_character_, NA_character_, 
                                                                                                                                                                                                              NA_character_, NA_character_, "Tom", NA_character_), 
               Name6 = c(NA_character_, "Narq", NA_character_, 
                                        NA_character_, "Seba", NA_character_), Name7 = c(NA_character_, 
                                                                                                                      NA_character_, "Greg", NA_character_, NA_character_, 
                                                                                                                      NA_character_), Name8 = c(NA_character_, 
                                                                                                                                                                            NA_character_, NA_character_, "Terry", NA_character_, 
                                                                                                                                                                            NA_character_), Name9 = c(NA_character_, 
                                                                                                                                                                                                                                NA_character_, NA_character_, NA_character_, "Coaty", 
                                                                                                                                                                                                                                NA_character_), Name10 = c(NA_character_, 
                                                                                                                                                                                                                                                                                                           NA_character_, "Meg", NA_character_, NA_character_, 
                                                                                                                                                                                                                                                                                                           NA_character_)), .Names = c("Name1", 
                                                                                                                                                                                                                                                                                                                                       "Name2", "Name3", 
                                                                                                                                                                                                                                                                                                                                       "Name4", "Name5", "Name6", 
                                                                                                                                                                                                                                                                                                                                       "Name7", "Name8", 
                                                                                                                                                                                                                                                                                                                                       "Name9", "Name10"
                                                                                                                                                                                                                                                                                                           ), row.names = c("1", "2", "3", "4", "5", "6"), class = "data.frame")

So I would like to remove all NAs from this data frame even it will create a data frame with different columns length.

Desired output:

  Name1  Name2 Name3 Name4 Name5 Name6 Name7 Name8 Name9 Name10
1  Mark Stefan   Max Titan   Tom  Narq  Greg Terry Coaty    Meg
2        Clara  Pete              Seba         
3               Gabe            
Shaxi Liver
  • 1,052
  • 3
  • 25
  • 47

2 Answers2

6

Actually you can achieve something similar to your desired output programmatically. Though I think that NAs are better than "" because they work with any class and easy to manipulate/operate on

First, we can define a function that will handle this

RemoveNAs <- function(x, size) {
  temp <- x[!is.na(x)]
  c(temp, rep("", size - length(temp)))
}

Then, calculate the longest Non-NA column size in the data

Max <- max(colSums(!is.na(df)))

Then, using data.table I would simply do

library(data.table)
setDT(df)[, lapply(.SD, RemoveNAs, Max)]
#    Name1  Name2 Name3 Name4 Name5 Name6 Name7 Name8 Name9 Name10
# 1:  Mark Stefan   Max Titan   Tom  Narq  Greg Terry Coaty    Meg
# 2:        Clara  Pete              Seba                         
# 3:               Gabe                                           

Which I believe is what you are trying to achieve, though as I said, it's better to have NAs instead of "" in that result IMO.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
-1

is.na(data) will give you a boolean list. This list will be TRUE is the entry in the list is <NA>. If you use data[boolean_list] = value entries in data that are TRUE will be replaced with value.

enter image description here

enter image description here]1

amblina
  • 353
  • 3
  • 7
  • 1
    This solution is already shared by @Deena – Ronak Shah Oct 28 '15 at 11:55
  • 3
    This doesn't give the desired output .... – Jaap Oct 28 '15 at 12:08
  • OP said that the desired output would be with the phrase: 'So I would like to remove all NAs from this data frame **even** it will create a data frame with different columns length.' This suggests OP wanted something that is akin to the desired output example wiithout having the different column lengths. – amblina Oct 28 '15 at 15:41