Sorry I'm not particularly articulate in the post title..I hope my example will be clearer!
If I start out with a data frame:
test.df <- data.frame(group=c(rep("a",4), rep("b",4)),
var=rep(1:4,2),
min= runif(8),
q25=runif(8,1,2),
q75=runif(8,2,3),
max=runif(8,3,4))
head(test.df,2)
group var min q25 q75 max
1 a 1 0.59078504 1.199138 2.119283 3.869486
2 a 2 0.06131107 1.676109 2.603068 3.739955
I know can melt it with id=c(group, var)
library(reshape2)
head(melt(test.df, id=c("group", "var")),2)
group var variable value
1 a 1 min 0.59078504
2 a 2 min 0.06131107
But what I'm looking for is a way to get two "value" columns by pairing min-max and q25-q75 so that it looks like:
group var variable value1 value2
1 a 1 min-max 0.59078504 3.869486
1 a 1 q25-q75 1.199138 2.119283
2 a 2 min-max 0.06131107 3.739955
2 a 2 q25-q75 1.676109 2.603068
I got a bit stuck on melt/cast and cant pull myself out, I'm sure there must be a neat way to accomplish this?
edit: this is a simplified example with only two pairs of variables - the idea is to solve this for larger numbers of pairs with minimal 'manual' work.
Table 1: Example Data Frame in R. Table 1 illustrates the structure of our data: It contains five rows and the three columns/variables x1, x2, and x3. Let’s reorder these variables… In the first example, you’ll learn how to reorder data frame columns by their index (i.e. the position of the variable within the data frame).
In R, we can use expand.grid function to create these combinations but to save it in a data frame, we would need to use as.data.frame function.
We simply have to open a squared bracket (i.e. []), write a comma (i.e. ,) to tell R that we want to change the columns, and specify a vector with the new ordering that we want to enforce (i.e. c (2, 1, 3)): Table 2: Reordered Data Frame. Table 2 shows the reordered data frame.
How to create a data frame with a column having repeated values in R? To create a data frame with a column having repeated values, we simply need to use rep function and we can repeat the values in a sequence of the values passed or repeating each value a particular number of times.
Another attempt:
newnames <- c("value1","value2")
data.frame(
test.df[c("group","var")],
variable=rep(c("min-max","q25-q75"),each=nrow(test.df)),
rbind(
setNames(test.df[c("min","max")],newnames),
setNames(test.df[c("q25","q75")],newnames)
)
)
Result:
group var variable value1 value2
1 a 1 min-max 0.6939545 3.479807
2 a 2 min-max 0.5646825 3.564637
3 a 3 min-max 0.3509824 3.928308
4 a 4 min-max 0.4217888 3.376821
5 b 1 min-max 0.6493916 3.933157
6 b 2 min-max 0.3978330 3.129940
7 b 3 min-max 0.4407376 3.707715
8 b 4 min-max 0.1651875 3.798546
9 a 1 q25-q75 1.3531055 2.242076
10 a 2 q25-q75 1.1811900 2.240188
11 a 3 q25-q75 1.3043822 2.695175
12 a 4 q25-q75 1.3315480 2.542576
13 b 1 q25-q75 1.2397527 2.107442
14 b 2 q25-q75 1.1973467 2.545511
15 b 3 q25-q75 1.9193746 2.502551
16 b 4 q25-q75 1.0425474 2.225601
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