replicate character values in a column

Hi friends, I have a problem manipulating data. Could you help me please?

I have this information:

ca_covid19 <- read.csv(file='https://raw.githubusercontent.com/pf0953-programaciongeoespacialr-2020/datos/master/covid19/casos/ca/2020_09_15_CSV_GENERAL.csv')

ca_covid19

And I have gathered it using gather from dply:

Covid <- gather(ca_covid19, factores, frecuencia, -1)
Covid

But I need to replicate each factor by frequency to get tow columns with character values (pais, factores), I used:

Covid %>%
  group_by(pais) %>%
  summarise(factor = rep(factores, frecuencia))

But doesn't work, return this: Error: Column factor must be length 1 (a summary value), not 3002.

Summarize work with length 1?

How can I solved to get:

Pais ________Factor
Belize::::::::::::::fallecimiento
Belize::::::::::::: fallecimiento
Belize::::::::::::::activo

thanks

Is this what you mean?

library(dplyr)
library(tidyr)

ca_covid19 <- read.csv(file='https://raw.githubusercontent.com/pf0953-programaciongeoespacialr-2020/datos/master/covid19/casos/ca/2020_09_15_CSV_GENERAL.csv')

ca_covid19 %>% 
    pivot_longer(cols = -pais, names_to = "factor") %>% 
    uncount(weights = value)
#> # A tibble: 898,010 x 2
#>    pais  factor    
#>    <chr> <chr>     
#>  1 BLZ   fallecidos
#>  2 BLZ   fallecidos
#>  3 BLZ   fallecidos
#>  4 BLZ   fallecidos
#>  5 BLZ   fallecidos
#>  6 BLZ   fallecidos
#>  7 BLZ   fallecidos
#>  8 BLZ   fallecidos
#>  9 BLZ   fallecidos
#> 10 BLZ   fallecidos
#> # … with 898,000 more rows

Created on 2020-09-18 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.