Reputation: 108
I'm trying to do the following in dplyr:
If I had a dataframe with sequences (df) and wanted to repeat the sequences in each row by n times (df2), how can I get a table that can do this in dplyr (answer = df2)? Thanks in advance!
df <- data.frame(start = c("ATG", "ATG", "ATG"),
seq = c("AAACCCTTT", "AAACCCTTT", "AAACCCTTT"),
stop = c("TAG", "TAG", "TAG"))
df1 <- data.frame(n = c("1", "2", "3"))
df2 <- data.frame(start = c("ATG", "ATGATG", "ATGATGATG"),
seq = c("AAACCCTTT", "AAACCCTTTAAACCCTTT", "AAACCCTTTAAACCCTTTAAACCCTTT"),
stop = c("TAG", "TAGTAG", "TAGTAGTAG"))
Upvotes: 1
Views: 667
Reputation: 887118
Using mutate
with across
library(dplyr)
df %>%
mutate(across(everything(), ~ strrep(., df1$n)))
Upvotes: 1
Reputation: 33488
Base R:
df2 <- df
df2[] <- mapply(strrep, df, df1)
df2
start seq stop
1 ATG AAACCCTTT TAG
2 ATGATG AAACCCTTTAAACCCTTT TAGTAG
3 ATGATGATG AAACCCTTTAAACCCTTTAAACCCTTT TAGTAGTAG
Upvotes: 2
Reputation: 1031
df <- data.frame(start = c("ATG", "ATG", "ATG"),
seq = c("AAACCCTTT", "AAACCCTTT", "AAACCCTTT"),
stop = c("TAG", "TAG", "TAG"))
df1 <- data.frame(n=1:3)
df2 <-
df %>%
bind_cols(df1) %>%
mutate_at(vars(start:stop),~strrep(.,n))
df2
Upvotes: 2