0

I have 7 data frames called:

"muestra_suplentes_103"
"muestra suplentes_108"
"muestra_suplentes_109"
"muestra_suplentes_113"
"muestra_suplentes_114"
"muestra_suplentes_138"
"muestra suplentes_139"

I also have a vector called:

cod_jer_groups= 103 108 109 113 114 138 139

I need to do this for this data frame:

muestra_suplentes_103$nrow=seq(from = 1, to = nrow(muestra_suplentes_103), by = 1)
muestra_suplentes_108$nrow=seq(from = 1, to = nrow(muestra_suplentes_108), by = 1)
muestra_suplentes_109$nrow=seq(from = 1, to = nrow(muestra_suplentes_109), by = 1)
muestra_suplentes_113$nrow=seq(from = 1, to = nrow(muestra_suplentes_113), by = 1)
muestra_suplentes_114$nrow=seq(from = 1, to = nrow(muestra_suplentes_114), by = 1)
muestra_suplentes_138$nrow=seq(from = 1, to = nrow(muestra_suplentes_138), by = 1)
muestra_suplentes_139$nrow=seq(from = 1, to = nrow(muestra_suplentes_139), by = 1)

I am traying to do this:

for(i in cod_jer_groups){
   muestra_suplentes$nrow= seq(from = 1, to = nrow(muestra_suplentes_i, by =  1))
   names(muestra_suplentes)[length(names(muestra_suplentes))]="nrow"
  }

I know that is wrong call "muestra_suplentes_i" but i dont know how to do it.

any suugestion?

thanks!

Natuk
  • 57
  • 1
  • 9

1 Answers1

1

Here is what I would do.

# put the data.frames into a named list, where names correspond to data.frame names
myList <- mget(ls(pattern="^muestra_suplentes")

# fill in the column values
myList <- lapply(myList, function(i) within(i, rowNum <- seq_len(nrow(i))))

To see more about why putting data.frames into rows can be a great idea, check out gregor's answer to this post.

Community
  • 1
  • 1
lmo
  • 37,904
  • 9
  • 56
  • 69