I have a data.frame
with varying numbers of rows per person. If a person has less than 4 rows I would like to give them 4 rows. I would like the extra rows to be a replicate of the the last row per person e.g., Tom below.
I know how to repeat rows by using this code:
dff <- df[rep(1:nrow(df), each=4), ]
But the problem here is that I have no argument for only replicating rows for IDs which occur less than 4 times.
I've tried this:
dff <- ifelse(length(df$noms < 4),
df[rep(1:nrow(df), each=4), ], df) ##doesn't work
I think I could use do.call(rbind, ...)
but again, I can't manage to integrate my argument into the code.
Here is something I've tried:
df <- ifelse(length(df$noms<4),
do.call(rbind, by(df, df$noms, rbind, 1:nrow(df))),
df) ## doesn't work
And also I have not managed to include an argument for how many repeats to do.
noms fruits apple orange kiwi all_comb comb
1 mary apple 1 0 0 1 1
2 mary grape 0 0 0 0 1
3 mary orange 0 1 0 0 1
4 mary apple 1 0 0 1 1
5 john banana 0 0 0 0 1
6 john apple 1 0 0 1 1
7 john apple 1 0 0 1 1
8 john apple 1 0 0 1 1
9 lucy kiwi 0 0 1 0 1
10 lucy orange 0 1 0 0 1
11 lucy apple 1 0 0 1 1
12 lucy berry 0 0 0 0 1
13 tom orange 0 1 0 0 1
Desired output
noms fruits apple orange kiwi all_comb comb
1 mary apple 1 0 0 1 1
2 mary grape 0 0 0 0 1
3 mary orange 0 1 0 0 1
4 mary apple 1 0 0 1 1
5 john banana 0 0 0 0 1
6 john apple 1 0 0 1 1
7 john apple 1 0 0 1 1
8 john apple 1 0 0 1 1
9 lucy kiwi 0 0 1 0 1
10 lucy orange 0 1 0 0 1
11 lucy apple 1 0 0 1 1
12 lucy berry 0 0 0 0 1
13 tom orange 0 1 0 0 1
14 tom orange 0 1 0 0 1
15 tom orange 0 1 0 0 1
16 tom orange 0 1 0 0 1
Here are some reproducible data:
noms <- as.character(c('mary', 'mary','mary','mary','john','john','john',
'john','lucy','lucy','lucy','lucy', 'tom'))
fruits <- as.character(c('apple','grape','orange','apple','banana',
'apple','apple','apple','kiwi','orange',
'apple','berry', 'orange'))
df <- data.frame(noms,fruits)
You're on the right track with ifelse
. Try the following:
noms <- as.character(c('mary', 'mary','mary','mary','john','john','john',
'john','lucy','lucy','lucy','lucy', 'tom'))
fruits <- as.character(c('apple','grape','orange','apple','banana',
'apple','apple','apple','kiwi','orange',
'apple','berry', 'orange'))
df <- data.frame(noms,fruits)
x <- with(df, ave(rep(1, nrow(df)), noms, FUN = length))
df[rep(rownames(df), ifelse(x >= 4, 1, 4)), ]
# noms fruits
# 1 mary apple
# 2 mary grape
# 3 mary orange
# 4 mary apple
# 5 john banana
# 6 john apple
# 7 john apple
# 8 john apple
# 9 lucy kiwi
# 10 lucy orange
# 11 lucy apple
# 12 lucy berry
# 13 tom orange
# 13.1 tom orange
# 13.2 tom orange
# 13.3 tom orange