Home > Blockchain >  How to repeat a vector of strings N times in a dataframe using dplyr
How to repeat a vector of strings N times in a dataframe using dplyr

Time:03-18

I am working with a list of dataframes and want to create a new column with the names of the variables. There are three variables and the length of the dataframe is 684, therefore I need the variable names to repeat 228 times. However, I can't get this to work.

Here is the snippet I am currently using:

empleo = lapply(lista.empleo, function(x){x = x %>%
        read_excel(skip=4) %>% 
        head(23) %>% 
        drop_na() %>%
        clean_names() %>% 
        pivot_longer(!1, 
                     names_to = 'fecha',
                     values_to = 'valor') %>% 
        mutate(variable = rep(c('trabajadores',
                                'masa',
                                'salario'), 
                              times = 228))})

So far, I have tried to use mutate, but I get the following mistake:

Error in `mutate()`:
! Problem while computing `variable = rep(c("trabajadores", "masa",
  "salario"), times = 228)`.
x `variable` must be size 0 or 1, not 684.

I will add the structure of a sample df in the comments since it is too big.

Thanks in advance for any help!

CodePudding user response:

The rep may fail as some datasets may have different number of rows in the list. Use length.out to make sure it returns n() elements (number of rows)

library(readxl)
library(tidyr)
library(dplyr)
library(janitor)
empleo <- lapply(lista.empleo, function(x){x = x %>%
        read_excel(skip=4) %>% 
        head(23) %>% 
        drop_na() %>%
        clean_names() %>% 
        pivot_longer(!1, 
                     names_to = 'fecha',
                     values_to = 'valor') %>% 
        mutate(variable = rep(c('trabajadores',
                                'masa',
                                'salario'), 
                               228, length.out = n()))})
  • Related