Reprex: Datapasta: Error in strrep(" ", char_length - nchar(char_vec)) : invalid 'times' value

Hello,

I am trying to create a reprex for a problem but my reprex is already giving me a hard time,
I get the error "Error in strrep(" ", char_length - nchar(char_vec)) : invalid 'times' value"
Would anyone please kindly advise what I am doing wrong?

Thank you!
Christine

```{r}
n <- 50
movie_frame<-sample_n(movies, n)

movie_frame <- movie_frame %>% 
select(title_type, imdb_rating)
datapasta::tribble_paste(movie_frame)

This is an issue with datapasta see this: "invalid 'times' value" error on factor column? · Issue #62 · MilesMcBain/datapasta · GitHub

I would try creating the dataframe with stringsAsFactors = FALSE or creating the tibble with no factor columns. Let me know if this helps.

2 Likes

You are trying to use datapasta and it is very useful. tribble_paste seems to have a hard time dealing with factors. You can try to convert them in string or use df_paste to create a code for a data.frame: tribble::df_paste(movie_frame). Did you try ?

For a reprex, you can also use a build-in dataframe already included in a package when possible, or you can use dput from base R.

1 Like

Hello Peter,
Thank you I see the problem. I am not very experienced in R, I am inputting the code incorrectly as it is still not working to generate my data. Could you please explain where in my code I use stringsAsFactors = FALSE ?

n <- 50
movie_frame<-sample_n(movies, n)
stringsAsFactors = FALSE

movie_frame <- movie_frame %>% 
select(title_type, imdb_rating)

Hello Christophe, thank you for your kind reply explanation. I tried dput but when I do not wish to have all the columns, I tried to select but it did not work, how do I use dput and select columns?

I am not sure how to correctly use tribble::df_paste(movie_frame). I tried
tribble::movies_paste(movie_frame)
datapasta::tribble_paste(movie_frame)

My frame is supposed to look something like this.

 data.frame(stringsAsFactors=FALSE,
                        title_type = c("Feature film","Feature film", "TV Movie", 
                                       "Documentary", "Documentary", "Feature film", "Feature film",  "Feature film", "TV Movie", "TV Movie", 
                                       "Feature film", "Feature film", "TV Movie", 
                                       "Documentary", "Documentary", "Feature film", "Feature film",  "Feature film", "TV Movie", "TV Movie"),
                        imdb_rating = c(2, 3, 2, 10, 8, 3, 3, 2, 8, 5, 2, 3, 2, 10, 8, 3, 3, 
                                        2, 8, 5))

Gidday.

@christinelly, I'm sorry you hit this problem with datapasta! As @pgensler has said this is an issue under active development, the error message is totally unrelated to the actual problem, which is quite poor on my part.

I think you are pretty close. @cderv was leading you in the right direction although I think he mistyped: If you use datapasta::df_paste() it will paste your data in the shape of a dataframe definition which can handle factor data.

Alternately you can just convert all factors to strings, and tribble_paste should work. Something like:

 movies %>%
 dplyr::mutate_if(is.factor, as.character) %>%
 datapasta::tribble_paste()

should do the trick.

edit: originally used purrr::map_if instead of dplyr::mutate_if 0_o

2 Likes

Oh yes I mistyped! I was thinking of tribble from tibble and I mixed up with datapasta... Thanks for correcting me.

@christinelly I am sorry. I meant datapaste::df_paste as @milesmcbain corrects it. In datapasta you have df_paste and tribble_paste

Sorry for misleading you. @milesmcbain got you covered. I hope you manage to do a reprex.

About dput, you have to use it at the end, at the time of generating the code. In you screen shot

head(movie, 10) %>%
    select(title_type, imdb_rating) %>%
    dput()

However, the returned code will be less nicer than with the great datapasta.

1 Like

Great thank you it runs like a dream!

1 Like

Got it:) Thank you also for the dput help, in case I run into more datapasta problems I have a solution!

2 Likes