I'm trying to simulate data for an experiment (learning purpose, self-taught) The purpose is to see how with the increase of distance (in cm) my variable decrease (I'm actually expecting that with the real data the decrease will be not linear). 5 replicate generated around my expected value, each indicated as e1, e2 etc.. and 4 distances indicated in cm
So this is the matrix:
dis2 <- rnorm(5, mean = 0.25, sd = 0.01)
dis12 <- rnorm(5, mean = 0.22, sd = 0.01)
dis24 <- rnorm(5, mean = 0.19, sd = 0.01)
dis36 <- rnorm(5, mean = 0.16, sd = 0.01)
mat <- matrix(c(dis2, dis12, dis24, dis36), ncol=5, byrow = TRUE)
tmat <- t(mat)
dfmat <- as.data.frame(tmat)
colnames(dfmat) <- c("2cm", "12cm", "24cm", "36cm")
rownames(dfmat) <- c("e1", "e2", "e3", "e4", "e5")
Now I want to build a 3 column dataframe where each measure has the value but also if those values belong to e1, e2 etc.. and if the measure is relative to 2cm, 12 cm etc
The closest I could come is by melt()
list <- as.list(dfmat)
melted <- melt(list)
Here I can obtain the respective distance and value measure but not at which experiment (e1, e2 etc..) belong
How can I add this parameter? I've tried to add the (e1, e2 etc..) as a factor but still can not manage to melt the list correctly
Any help would be great
Using the tidyverse
framework, we can move the rownames into a column then pivot appropriately.
library(dplyr)
library(tibble)
library(tidyr)
dfmat %>%
as.data.frame() %>%
rownames_to_column("experiment") %>%
gather('decrease', 'val', `2cm`:`36cm`)
experiment decrease val
1 e1 2cm 0.2602702
2 e2 2cm 0.2446041
3 e3 2cm 0.2652482
4 e4 2cm 0.2292223
5 e5 2cm 0.2431740
6 e1 12cm 0.2203435
7 e2 12cm 0.2324304
8 e3 12cm 0.2299671
9 e4 12cm 0.2113966
10 e5 12cm 0.2186454
11 e1 24cm 0.1847289
12 e2 24cm 0.1999656
13 e3 24cm 0.1766850
14 e4 24cm 0.1946892
15 e5 24cm 0.1902934
16 e1 36cm 0.1594713
17 e2 36cm 0.1574840
18 e3 36cm 0.1763698
19 e4 36cm 0.1651663
20 e5 36cm 0.1517848
That said, you may be able to skip the round-about way of making a test set with:
dat <- data.frame(
experiment = rep(c("e1", "e2", "e3", "e4", "e5"), 4),
decrease = rep(c("2cm", "12cm", "24cm", "36cm"), each = 5),
val = c(dis2, dis12, dis24, dis36)
)
> dat
experiment decrease val
1 e1 2cm 0.2602702
2 e2 2cm 0.2446041
3 e3 2cm 0.2652482
4 e4 2cm 0.2292223
5 e5 2cm 0.2431740
6 e1 12cm 0.2203435
7 e2 12cm 0.2324304
8 e3 12cm 0.2299671
9 e4 12cm 0.2113966
10 e5 12cm 0.2186454
11 e1 24cm 0.1847289
12 e2 24cm 0.1999656
13 e3 24cm 0.1766850
14 e4 24cm 0.1946892
15 e5 24cm 0.1902934
16 e1 36cm 0.1594713
17 e2 36cm 0.1574840
18 e3 36cm 0.1763698
19 e4 36cm 0.1651663
20 e5 36cm 0.1517848