Home > Net >  R matrix NA remove from cells
R matrix NA remove from cells

Time:09-26

I'm pretty new to R, and I have a project where I have a df in which there are a lot of NA values. My aim is to remove the NA-s from the cells. My problem is that I only found sollutions to remove the whole row or column.

My dataframe looks like:

  V1   V2   V3
1 1    2    NA
2 NA   1.1  NA
3 2.1  1    NA
4 3.4  NA   5

And so on...

My goal is to get something like this:

    V1    V2   V3
   1 1    2    
   2 1.1  
   3 2.1  1    
   4 3.4  5   

So basically I want all my values to start from the same position. Its not a problem if the NA-s go the end, I just need it to everything will be below each other.

So far I tryed na.omit(), na.rm and complete.case, but non of them seemd to help me.

Any help would be appreciated!

CodePudding user response:

Dataframes are by definition rectangular data forms, and so you can't remove individual cells and still keep at as a dataframe. In the comments you indicate that moving the NAs to the end of the row would be an acceptable solution.

You can move the non-NA values to the start of each row with the following code:

df <- data.frame(V1=c(1, NA, 2.1, 3.4), V2=c(2,1.1,1,NA), V3=c(NA,NA,NA,5))

df[] <- t(apply(df,1,function(x) c(x[!is.na(x)],x[is.na(x)])))

#Show output
df
#    V1 V2 V3
# 1 1.0  2 NA
# 2 1.1 NA NA
# 3 2.1  1 NA
# 4 3.4  5 NA

CodePudding user response:

Is it just that you want the data to print with spaces instead of NA, if so, then this will do it:

tib <- tibble::tribble(
  ~V1,   ~V2,   ~V3,
1,    2,    NA,
NA,   1.1,  NA,
2.1,  1,    NA,
3.4, NA,   5) %>% as.data.frame()

tib %>% 
  mutate(across(everything(), 
                ~case_when(is.na(.x) ~ "", 
                           TRUE ~ as.character(.x)))) %>% 
  noquote(.)
#    V1  V2 V3
# 1   1   2   
# 2     1.1   
# 3 2.1   1   
# 4 3.4      5

Otherwise, if you want to do this so that other functions in R will somehow disregard then missing data, then this will not work.

  • Related