I'm struggling with coming up with the correct process to transform some data I'm doing analysis on without resorting to a scripting language.
The data takes a format similar to the following
data.frame(Group=LETTERS[1:3],Total=c(100,120,130),Modified=c(12,15,32))
Group Total Modified
1 A 100 12
2 B 120 15
3 C 130 32
I'd like the resulting data frame to look like
+-------+----------+
| Group | Modified |
+-------+----------+
| A | Y |
| A | Y |
| A | Y |
| . | . |
| . | . |
| . | . |
| A | N |
| A | N |
| B | Y |
| B | Y |
| . | . |
| . | . |
| . | . |
| B | N |
+-------+----------+
There should be 12 rows with Group A and Modified = Y and 88 rows with Group A and Modified = N. Same goes for B, C, etc.
In most cases there are additional columns that will need to be repeated on each row along with the Group info.
You can use rep
with the appropriate times
argument.
A data.table
solution for coding elegance
library(data.table)
# your data is in the data.frame DF
DF <- data.table(DF)
levels <- c('Y', 'N')
DF[,list(Modified = rep(levels,c(Modified,Total-Modified))),by = Group]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With