Axel K
Axel K

Reputation: 191

Overview over not empty Cols by name by Grouping in R data frame

I have a question that seems simple to me, but I can't solve it in R.

I have a data set of products with a classification and multiple characteristic columns. There I would like to group the data record according to classification and have the characteristics output that are not NA.

i.E.:

Classification characteristics 1 characteristics 2
Type1 value1 value1
Type2 NA value2
Type1 value2 value3
Type2 NA value4

The expected result looks like:

Classification characteristics
Type1 characteristics 1
Type1 characteristics 2
Type2 characteristics 1

Thanks a lot for your help!

Upvotes: 0

Views: 34

Answers (1)

wernor
wernor

Reputation: 421

Maybe something like this?

Using the dplyr approach:

df <- tibble(Classification=c('Type1','Type2','Type1','Type2'),
           characteristics1=c("value1",NA,"value2",NA),
           characteristics2=c('value1','value2','value3','value4'))

df_new <- df %>% 
           pivot_longer(-Classification,
                        names_to='characteristics',
                        values_drop_na=TRUE) %>% 
           select(-value) %>% 
           distinct(Classification,characteristics)

That gives out

# A tibble: 3 × 2
  Classification characteristics 
  <chr>          <chr>           
1 Type1          characteristics1
2 Type1          characteristics2
3 Type2          characteristics2

Type2 is different but "characteristics1" is NA in both rows from your sample data and you were asking for non-NA cases.

Upvotes: 0

Related Questions