Reputation: 1
I've googled a bunch and could find an answer anywhere but that maybe that I couldn't explain it well in few words.
Coffee| |Steve
Coffee| Cup | |
Turkey| Fork| |
|Fork |Mike
I'd like tho take the data in these four rows and consolidate it into two rows so it is displayed.
Coffee|Cup|Steve
Turkey|Fork|Mike
I'd appreciate any help. I am brand new to code. I am saw it being benefical and quicker at work in the long run.
Thanks
Upvotes: 0
Views: 57
Reputation: 2189
Using library(tidyverse)
and making bold assumptions about your data set:
df <- data.frame(food = c("Coffee","Coffee","Turkey", NA),
utensil = c(NA, "Cup", "Fork", "Fork"),
name = c("Steve", NA, NA, "Mike"))
df <- df %>%
group_by(food) %>%
arrange(utensil) %>%
fill(utensil) %>%
group_by(utensil) %>%
arrange(food) %>%
fill(food) %>%
drop_na()
Upvotes: 1
Reputation: 1079
I think it's best to decompose the table into a list, remove NA values, then take unique. Then you can just convert back to a data frame. I am using read_r
to make a data frame here, but the rest is Base R.
myTab <- readr::read_csv("h1,h2,h3
Coffee,,Steve
Coffee,Cup,
Turkey,Fork,
,Fork,Mike")
myTab
## # A tibble: 4 x 3
## h1 h2 h3
## <chr> <chr> <chr>
## 1 Coffee NA Steve
## 2 Coffee Cup NA
## 3 Turkey Fork NA
## 4 NA Fork Mike
myTab <- lapply(myTab, na.omit)
myTab <- lapply(myTab, as.character)
myTab <- lapply(myTab, unique)
myTab <- data.frame(myTab)
myTab
## h1 h2 h3
## 1 Coffee Cup Steve
## 2 Turkey Fork Mike
Upvotes: 0