library(dplyr)
library(tidyr)
# create example data
df <- data.frame(pollinator = c(rep(4, 8), rep(5, 6), rep(6,2)),
flower_id = sample(c("H", "L"), 16, replace=TRUE))
# create a unique id by group
# transpose the dataframe
# join all the columns and rename it as sequence
# clean up the joined columns
df %>%
group_by(pollinator) %>%
mutate(n = 1:n(),
id = paste0(pollinator, flower_id, n)) %>%
pivot_wider(-n, names_from = id, values_from = flower_id) %>%
unite(sequence, -pollinator, sep=" ") %>%
mutate(sequence = trimws(gsub("NA", "", sequence)))
1条答案
按热度按时间c2e8gylq1#
这似乎是一种奇怪的输出格式,但这里有一个例子,说明如何提供代码并(希望)得到答案。