简体   繁体   中英

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

  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]. 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.

You need to call the matrix and not the dataframe.. ie df[,2] will drop the list attribute:

You are looking for:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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