I have a dataframe in R that looks like this:
Word Base Number Type
- - - -
shoe shoe 4834 singular
shoes shoe 49955 plural
toy toy 75465 singular
toys toy 23556 plural
key key 39485 singular
keys key 6546 plural
jazz jazz 58765 plural
I would like to transform it so that it looks like this:
Word_Sg Word_Pl Base Num_Singular Num_Plural
-- -- -- -- --
shoe shoes shoe 4834 49955
toy toys toy 75465 23556
key keys key 39485 6546
NA jazz jazz NA 58765
So rather than having two rows for the values for singular & plural, I want to have two colums, one with the number for singular, and one with the number for plural.
I've tried a few things using dplyr::summarize
, but so far, without any success. Here is the code that I've come up with so far:
dataframe1 <- dataframe %>%
mutate(Num_Singular = case_when(Type == "singular" ~ Number)) %>%
mutate(Num_Plural = case_when(Type == "plural" ~ Number)) %>%
dplyr::select(Word, Base, Num_Singular, Num_Plural) %>%
group_by(Base) %>%
dplyr::summarize(Num_Singular = paste(na.omit(Num_Singular)),
Num_Plural = paste(na.omit(Num_Plural))
However, it gives me this error:
Error in summarise_impl(.data, dots) :
Column `Num_Singular` must be length 1 (a summary value), not 2)
I think the problem might be that there are rows that don't necessarily have singular AND plural, but only either (e.g. "jazz"). Most rows have both though.
So how can I do this in R or dplyr?