简体   繁体   中英

dplyr : Error in mutate_impl(.data, dots) : argument 'times' incorrect

I have a tibble as :

# A tibble: 2 × 2
  read_seq unique_id
     <chr>     <dbl>
1 AATTGGCC         1
2   GGGTTT         2

I want to create a new variable containing a string of the same size as read_seq. I did that but have an error:

> r %>% mutate(y=paste(rep("H",width(read_seq)),sep=""))
Error in mutate_impl(.data, dots) : argument 'times' incorrect

When I only try to catch read_seq width it works :

> r %>% mutate(y=width(read_seq))
# A tibble: 2 × 3
  read_seq unique_id     y
     <chr>     <dbl> <int>
1 AATTGGCC         1     8
2   GGGTTT         2     6

Here's the dput() of the example for reproducibility :

r <- structure(list(read_seq = c("AATTGGCC", "GGGTTT"), unique_id = c(1, 
2)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-2L), .Names = c("read_seq", "unique_id"))

The error comes from rep , the times parameter needs to be either 1 or the same length as x , which is not the case here:

rep("H", c(1,2))
Error in rep("H", c(1, 2)) : invalid 'times' argument

You can use strrep instead, which recycles x and times as necessary:

r %>% mutate(y = strrep("H",nchar(read_seq)))

# A tibble: 2 x 3
#  read_seq unique_id        y
#     <chr>     <dbl>    <chr>
#1 AATTGGCC         1 HHHHHHHH
#2   GGGTTT         2   HHHHHH

Or if you need some random strings of equal sizes, use stringi::stri_rand_strings :

r %>% mutate(y = stri_rand_strings(length(read_seq), nchar(read_seq)))

# A tibble: 2 x 3
#  read_seq unique_id        y
#     <chr>     <dbl>    <chr>
#1 AATTGGCC         1 H0flAbNS
#2   GGGTTT         2   QrQOLj

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM