Reputation: 95
I'm trying to create a dataframe that spreads my dataframe from this:
my_data1 <- data.frame(Tank = c("1A", "1D", "1F", "1A", "1F", "2A"), Bag = c("C1", "C2", "C2", "C1", "C2", "T1"), name = c("ally", "ally", "nancy", "ted", "mark", "ted"), deformity=c("spinal", "eye", "snout", "spinal", "head", "eye"))
to this:
my_data2 <- data.frame(Tank = c("1A", "1D", "1F", "2A"), Bag = c("C1", "C2", "C2", "T1"), ally_deformity = c("spinal", "eye", NA, NA), nancy_deformity=c(NA, NA, "snout", NA), ted_deformity=c("spinal", NA, NA, "eye"), mark_deformity=c(NA, NA, "head", NA))
So I want the columns "Tank" and "Bag" to be my unique identifiers and every other column to be the deformities each individual person found for that specific tank and bag. I've been trying tidyr's spread with no luck.
Upvotes: 1
Views: 55
Reputation: 101343
A data.table
option using dcast
dcast(setDT(my_data1), Tank + Bag ~ paste0(name, "_deformity"))
gives
Tank Bag ally_deformity mark_deformity nancy_deformity ted_deformity
1: 1A C1 spinal <NA> <NA> spinal
2: 1D C2 eye <NA> <NA> <NA>
3: 1F C2 <NA> head snout <NA>
4: 2A T1 <NA> <NA> <NA> eye
Upvotes: 0
Reputation: 79228
Using base R, you could do:
reshape(my_data1, v.names = "deformity", timevar = "name", dir="wide", idvar = "Tank", sep="_")
Tank Bag deformity_ally deformity_nancy deformity_ted deformity_mark
1 1A C1 spinal <NA> spinal <NA>
2 1D C2 eye <NA> <NA> <NA>
3 1F C2 <NA> snout <NA> head
6 2A T1 <NA> <NA> eye <NA>
Upvotes: 1
Reputation: 887118
We can use pivot_wider
library(dplyr)
library(tidyr)
library(stringr)
my_data1 %>%
mutate(name = str_c(name, '_deformity')) %>%
pivot_wider(names_from = name, values_from = deformity)
-output
# A tibble: 4 x 6
# Tank Bag ally_deformity nancy_deformity ted_deformity mark_deformity
# <chr> <chr> <chr> <chr> <chr> <chr>
#1 1A C1 spinal NA spinal NA
#2 1D C2 eye NA NA NA
#3 1F C2 NA snout NA head
#4 2A T1 NA NA eye NA
Upvotes: 1