Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commands to transform data.frame in R

Tags:

dataframe

r

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.

like image 261
Garrett Avatar asked Dec 12 '22 17:12

Garrett


1 Answers

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]
like image 187
mnel Avatar answered Jan 02 '23 14:01

mnel