简体   繁体   English

在R中访问矩阵的元素

[英]Accessing the elements of a matrix in R

I have a data frame which is the output obtained after using aggregate function, 我有一个数据框,它是使用聚合函数后获得的输出,

 >df
  Type     mean        sd
1 A        76.088250   3.233297
2 B        78.403967   4.144146
3 C        102.855533  44.775766

The second and the third columns are stored as a matrix. 第二和第三列存储为矩阵。 For example, 例如,

>df[2] 
  mean        sd
1 76.088250   3.233297
2 78.403967   4.144146
3 102.855533  44.775766

I want to merge the first and second column of df[2] with a symbol in between, ie the desired output is 我想将df [2]的第一和第二列与它们之间的符号合并,即所需的输出是

  mean ± sd
1 76.088250 ± 3.233297
2 78.403967 ± 4.144146
3 102.855533 ± 44.775766 

To do this, I want to access the first column and the second column of df[2]. 为此,我要访问df [2]的第一列和第二列。 Probably a very simple question, but I am not able to find a way to do this. 可能是一个非常简单的问题,但我找不到解决方法。 I tried the following, 我尝试了以下方法

Matrix <- df[2]
print(Matrix[,1])

The output is 输出是

          mean        sd
[1,]  76.08825  3.233297
[2,]  78.40397  4.144146
[3,] 102.85553 44.775766

Expected output, 预期的输出,

    mean
1  76.08825 
2  78.40397
3 102.85553

I would like to ask for help. 我想寻求帮助。 Excuse me for the naive question, I'm a beginner in R. 请问我一个幼稚的问题,我是R语言的初学者。

You need to call the matrix and not the dataframe.. ie df[,2] will drop the list attribute: 您需要调用矩阵,而不是数据帧。即df[,2]将删除列表属性:

You are looking for: 您正在寻找:

paste(df[,2][,1],intToUtf8(177),df[,2][,2])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM