r - Converting table to a matrix -
in image, want arrange table (on left-side) table (on right-side) containing 3 rows.
https://drive.google.com/file/d/0b4ggtf6nyi4ymhltwjrkedhob3m/view?usp=sharing
that is, have table this
0 3 6 9 13 16 31 64 n 100,0 98,7 96,7 97,5 91,2 15,7 0,4 0,6 n1 100,0 102,0 97,8 98,6 89,8 11,0 0,3 0,2
and want arrange this:
alkanes time degradation n 0 100,0 n 3 98,7 n 6 96,7 n 9 97,5 n 13 91,2 n 16 15,7 n 31 0,4 n 64 0,6 n1 0 100,0 n1 3 102,0 n1 6 97,8 n1 9 98,6 n1 13 89,8 n1 16 11,0 n1 31 0,3 n1 64 0,2
sample data:
x <- structure(list(x = structure(1:3, .label = c("n", "n1", "n2"), class = "factor"), x0 = c(100, 100, 100), x3 = c(98.7, 102, 95.1), x6 = c(96.7, 97.8, 94.5), x9 = c(97.5, 98.6, 101), x13 = c(91.2, 89.8, 89.4), x16 = c(15.7, 11, 22.5), x31 = c(0.4, 0.3, 0), x64 = c(0.6, 0.2, 0)), .names = c("x", "x0", "x3", "x6", "x9", "x13", "x16", "x31", "x64"), class = "data.frame", row.names = c(na, -3l))
desired output:
y <- structure(list(alkanes = structure(c(1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l), .label = c("n", "n1", "n2"), class = "factor"), time = c(0l, 3l, 6l, 9l, 13l, 16l, 31l, 64l, 0l, 3l, 6l, 9l, 13l, 16l, 31l, 64l, 0l, 3l, 6l, 9l, 13l, 16l, 31l, 64l), degradation = c(100, 98.7, 96.7, 97.5, 91.2, 15.7, 0.4, 0.6, 100, 102, 97.8, 98.6, 89.8, 11, 0.3, 0.2, 100, 95.1, 94.5, 101, 89.4, 22.5, 0, 0)), .names = c("alkanes", "time", "degradation"), class = "data.frame", row.names = c(na, -24l))
given "x" as:
x # x x0 x3 x6 x9 x13 x16 x31 x64 # 1 n 100 98.7 96.7 97.5 91.2 15.7 0.4 0.6 # 2 n1 100 102.0 97.8 98.6 89.8 11.0 0.3 0.2 # 3 n2 100 95.1 94.5 101.0 89.4 22.5 0.0 0.0
you can try like:
as.data.frame( as.table( `dimnames<-`(as.matrix(x[-1]), list(x[[1]], gsub("x", "", names(x)[-1]))))) # var1 var2 freq # 1 n 0 100.0 # 2 n1 0 100.0 # 3 n2 0 100.0 # 4 n 3 98.7 # 5 n1 3 102.0 # 6 n2 3 95.1 # 7 n 6 96.7 # 8 n1 6 97.8 # 9 n2 6 94.5 # 10 n 9 97.5 # 11 n1 9 98.6 # 12 n2 9 101.0 # 13 n 13 91.2 # 14 n1 13 89.8 # 15 n2 13 89.4 # 16 n 16 15.7 # 17 n1 16 11.0 # 18 n2 16 22.5 # 19 n 31 0.4 # 20 n1 31 0.3 # 21 n2 31 0.0 # 22 n 64 0.6 # 23 n1 64 0.2 # 24 n2 64 0.0
from there, it's sorting , renaming columns, standard operations.
Comments
Post a Comment