how do you removed NAs from a column without affecting others?


How can I removed NAs from the above dataframe?
Here is the expected output:


This may sound like a strange question, but are you sure you want to do that? Does the data always come exactly in pairs that get repeated? In general, if you remove the NAs from each column separately you change which row the good data is associated with and you may end up with a different number of cells in each column, which isn't permitted.

Yes, this is the reprex of my datasets.

Below is one way to achieve your desired output using pivot_longer and pivot_wider.



xyz = xyz |>
  pivot_longer(cols = -'name', names_to = 'group', names_repair = 'minimal') |>
  filter(! |>
  pivot_wider(names_from = group, values_from = value)

#> # A tibble: 2 × 4
#>   name  maths science history
#>   <chr> <dbl>   <dbl>   <dbl>
#> 1 a         7       6       6
#> 2 b         8       8       7

Created on 2022-12-03 with reprex v2.0.2.9000

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.