按列名将表转换为矩阵 [英] Convert table into matrix by column names
本文介绍了按列名将表转换为矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有如下所示的数据框
models cores time
1 4 1 0.000365
2 4 2 0.000259
3 4 3 0.000239
4 4 4 0.000220
5 8 1 0.000259
6 8 2 0.000249
7 8 3 0.000251
8 8 4 0.000258
...等
我想将它转换为一个表/矩阵,其中行标签为#models,列标签为#cores,数据条目为时间
I would like to convert it into a table/matrix with #models for rows labels, the #cores for the columns labels and time as the data entries
例如
1 2 3 4 5 6 7 8
1 time data
4 time data
目前我正在使用 for 循环将其转换为这种结构,但我想知道是否有更好的方法?
currently I'm using for loops to convert it into this structure, though I am wondering if there was a better method?
推荐答案
检查从 reshape 包转换的方法
Check method cast from reshape package
# generate test data
x <- read.table(textConnection('
models cores time
4 1 0.000365
4 2 0.000259
4 3 0.000239
4 4 0.000220
8 1 0.000259
8 2 0.000249
8 3 0.000251
8 4 0.000258'
), header=TRUE)
library(reshape)
cast(x, models ~ cores)
结果:
models 1 2 3 4
1 4 0.000365 0.000259 0.000239 0.000220
2 8 0.000259 0.000249 0.000251 0.000258
这篇关于按列名将表转换为矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文