Put new copies of rows depend of count variable

Hi community

Im have the count occurs data frame about other data frame. In Freq I show the times of this ACESION appear.

ACCESION Freq
 G  838    2
 G  938    3

The other data frame is this:

DT <-  structure(list(ACCESION = c("G  838", "G  938"), 
GENERO = c("Phaseolus", "Phaseolus"), 
ESPECIE = c("vulgaris", "vulgaris"),
LATITUD = c(19.43, 16.75), 
LONGITUD = c(-98.17, -96.74), ALTURA = c(2660, 1800)), row.names = 1:2, class = "data.frame")

####################################################
ACCESION    GENERO  ESPECIE LATITUD LONGITUD ALTURA
  G  838 Phaseolus vulgaris   19.43   -98.17   2660
  G  938 Phaseolus vulgaris   16.75   -96.74   1800

In order of Freq, I need put this copies in each correspond ACCESION in DT. Like this:
For example, like Freq is 2 for G 838, I need repeat 2 time this whole row. For G 938 are 3 times.

ACCESION    GENERO  ESPECIE LATITUD LONGITUD ALTURA
  G  838 Phaseolus vulgaris   19.43   -98.17   2660
  G  838 Phaseolus vulgaris   19.43   -98.17   2660
  G  938 Phaseolus vulgaris   16.75   -96.74   1800
  G  938 Phaseolus vulgaris   16.75   -96.74   1800
  G  938 Phaseolus vulgaris   16.75   -96.74   1800

Because make this one by one is very time consuming. I have 3000 rows.

Tnks!

This can be accomplished with the expandRows() function from the splitstackshape package.

library(tidyverse)
library(splitstackshape)

Freq = data.frame(ACCESION = c('G  838', 'G  938'),
                  Freq = c(2, 3))

DT <-  structure(list(ACCESION = c("G  838", "G  938"), 
                      GENERO = c("Phaseolus", "Phaseolus"), 
                      ESPECIE = c("vulgaris", "vulgaris"),
                      LATITUD = c(19.43, 16.75), 
                      LONGITUD = c(-98.17, -96.74), 
                      ALTURA = c(2660, 1800)), 
                 row.names = 1:2, 
                 class = "data.frame")


DT %>%
  left_join(Freq) %>%
  expandRows(count = 'Freq', count.is.col = T)
#> Joining, by = "ACCESION"
#>     ACCESION    GENERO  ESPECIE LATITUD LONGITUD ALTURA
#> 1     G  838 Phaseolus vulgaris   19.43   -98.17   2660
#> 1.1   G  838 Phaseolus vulgaris   19.43   -98.17   2660
#> 2     G  938 Phaseolus vulgaris   16.75   -96.74   1800
#> 2.1   G  938 Phaseolus vulgaris   16.75   -96.74   1800
#> 2.2   G  938 Phaseolus vulgaris   16.75   -96.74   1800

Created on 2022-10-12 with reprex v2.0.2.9000

1 Like

Im find other way with merge two data frame.

count <- data.frame(ACCESION=c('G  838','G  938'),
                                           Freq=c(2,3))

DT <-  structure(list(ACCESION = c("G  838", "G  938"), 
GENERO = c("Phaseolus", "Phaseolus"), 
ESPECIE = c("vulgaris", "vulgaris"),
LATITUD = c(19.43, 16.75), 
LONGITUD = c(-98.17, -96.74), ALTURA = c(2660, 1800)), row.names = 1:2, class = "data.frame")
DT_FULL<- DT%>% 
  merge(count , by = "ACCESION")

DT_FULL<- DT_FULL[rep(seq.int(1,nrow(DT_FULL)), DT_FULL$Freq),]

# ACCESION      GENERO  ESPECIE    LATITUD LONGITUD ALTURA  Freq
# 1     G  838 Phaseolus vulgaris   19.43   -98.17   2660    2
# 1.1   G  838 Phaseolus vulgaris   19.43   -98.17   2660    2
# 2     G  938 Phaseolus vulgaris   16.75   -96.74   1800    3
# 2.1   G  938 Phaseolus vulgaris   16.75   -96.74   1800    3
# 2.2   G  938 Phaseolus vulgaris   16.75   -96.74   1800    3
1 Like

Yes!, this is the output. Tnks!

You can do this with tidyr too (wich is part of tidyverse)

library(tidyverse)

Freq <- data.frame(ACCESION = c('G  838', 'G  938'),
                   Freq = c(2, 3))

DT <-  structure(list(ACCESION = c("G  838", "G  938"), 
                      GENERO = c("Phaseolus", "Phaseolus"), 
                      ESPECIE = c("vulgaris", "vulgaris"),
                      LATITUD = c(19.43, 16.75), 
                      LONGITUD = c(-98.17, -96.74), 
                      ALTURA = c(2660, 1800)), 
                 row.names = 1:2, 
                 class = "data.frame")


DT %>%
    left_join(Freq) %>%
    uncount(Freq)
#> Joining, by = "ACCESION"
#>   ACCESION    GENERO  ESPECIE LATITUD LONGITUD ALTURA
#> 1   G  838 Phaseolus vulgaris   19.43   -98.17   2660
#> 2   G  838 Phaseolus vulgaris   19.43   -98.17   2660
#> 3   G  938 Phaseolus vulgaris   16.75   -96.74   1800
#> 4   G  938 Phaseolus vulgaris   16.75   -96.74   1800
#> 5   G  938 Phaseolus vulgaris   16.75   -96.74   1800

Created on 2022-10-12 with reprex v2.0.2

2 Likes

This topic was automatically closed 7 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.