1

Suppose if this is my dataset

  Id    Name     Score     Fear     Satisfaction
  1     A        10        NA       NA
  1     B        11        NA       NA
  1     A        NA        23.2     NA
  1     B        NA        34.1     NA
  1     A        NA        NA       42.11
  1     B        NA        NA       31.19

How can I cleanup this dataset such that all rows are aligned correctly like this below and there aren't too many unnecessary NAs like this below

 Id    Name     Score     Fear     Satisfaction
  1     A        10        23.2     42.11
  1     B        11        34.1     31.19

I apologize if I was unable to explain this more accurately but I assume the example should help. Thanks in advance.

Emily Fassbender
  • 413
  • 5
  • 15

2 Answers2

1

If you have a limited number of columns, then I believe this is the best way:

SELECT * 
FROM <table_name> 
WHERE 
       Score IS NOT NULL
   AND Fear IS NOT NULL 
   AND Satisfaction IS NOT NULL
Siddharth Jain
  • 118
  • 1
  • 14
  • Not good at SQL: What does `Score IS NOT` do? I also think this might be inefficient if you have so many conditions. – NelsonGon Oct 01 '19 at 05:58
0

Assuming you have equal number of non-NA values per group and per column as shown in the example, we cangroup_by Id and Name and remove NA values

library(dplyr)

df %>%  group_by(Id, Name) %>%  summarise_all(na.omit)

#     Id Name  Score  Fear Satisfaction
#  <int> <fct> <int> <dbl>        <dbl>
#1     1 A        10  23.2         42.1
#2     1 B        11  34.1         31.2
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213