Reputation: 147
I have a large dataframe with an ID column, the first 8208 rows have ID 1, the next 432 rows have ID 2, the next 8208 rows have ID 3, the next 432 rows have ID 3 etc.
Now I would like to rename the IDs so that the first 432 rows have ID 1.1, the next 432 rows ID 1.2 etc - until 1.19 for rows 7777:8208. Then the next 432 rows remain with ID 2, for the next 8208 the same should be done: ID 2.1, 2.2, etc. until 2.19.
I already tried
split(x, seq(1:19))
and then
tapply(df, split)
but this changes the order of the rows and obviously does not rename the ID column. So basically only the odd IDs need to be changed by splitting them in 19 equal parts, keeping row order the same and renaming those parts so that the original ID is added with .1, .2, .3 etc until .19
Upvotes: 1
Views: 218
Reputation:
Think this work too, and is a bit easier on the eye. It gives a warning but only because not all values are replaced
within(df2, id[df2$id %% 2 != 0] <- paste(id, rep(1:19, 432), sep = '.'))
Upvotes: 1
Reputation: 4761
You could try:
df[df$id%%2!=0,"id"]=paste0(df[df$id%%2!=0,"id"],".",rep(rep(seq(1,19),each=432),length(df[df$id%%2!=0,"id"])/8208))
I made the assumption that you have multiple of 8208 rows.
Upvotes: 0