官术网_书友最值得收藏!

Transposing matrices

One of the most used, but often not mentioned, methods for restructuring data is transposing matrices. This simply means switching the columns with rows and vice versa, via the t function:

> (m <- matrix(1:9, 3))
 [,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> t(m)
 [,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9

Of course, this S3 method also works with data.frame, and actually, with any tabular object. For more advanced features, such as transposing a multi-dimensional table, take a look at the aperm function from the base package.

主站蜘蛛池模板: 莒南县| 安岳县| 天水市| 天全县| 罗江县| 盐边县| 长宁区| 包头市| 科尔| 宁夏| 砚山县| 饶平县| 星子县| 阿拉善盟| 庆城县| 彭州市| 河西区| 汉川市| 富川| 平江县| 临漳县| 喜德县| 米脂县| 孟连| 翁牛特旗| 广安市| 思茅市| 彭山县| 鲜城| 于都县| 花莲市| 德惠市| 绍兴市| 阜阳市| 平阴县| 凤城市| 启东市| 姜堰市| 石城县| 吉安市| 永嘉县|