1

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!

pahi
  • 95
  • 5

2 Answers2

1

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
Miff
  • 7,486
  • 20
  • 20
0

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.

DaveArmstrong
  • 18,377
  • 2
  • 13
  • 25
  • I would like my values to move under each other. Like I wrote in my questin, my aim is remove the NA and move the values to the places where NA removed. Like if in a row there are 3 NA and then 3 values, I would like to have a row starting with the value, not an NA or white space. BTW this code is really helpfull, in other matters of mine :) – pahi Sep 25 '21 at 13:19