0
votes

My goal is to convert a column from a data table in R into a matrix so that I may incorporate it into later use in my project. From a dataset, I saved all of the values from the 'AGE' column into a variable:

AGE.data <- sample_data(phy.norm.new)[,'AGE']

Doing so yielded:

Sample Data: [240 samples by 1 sample variables]:

AGE

AGE.109 56

AGE.236 50

AGE.201 34

AGE.96 64

AGE.21 66

AGE.56 54

AGE.64 65

AGE.225 76

AGE.24 70

AGE.57 84

AGE.117 50

AGE.177 65

...

(Not all rows have been typed in this blockquote, only a few in order to illustrate what it looks like in the console).

In order to convert the values from this column into a matrix, I used:

mat.AGE.data <- matrix(AGE.data)

However, I do not get a 240-row by 1-column matrix displaying all of the age values. Instead, this displays in the console.

mat.AGE.data

     [,1]       
[1,] Integer,240

I've read the comprehensive R tutorial, but I'm still unsure of how to resolve this issue. Thanks in advance.

Edit: Using str(AGE.data) I got this output:

'data.frame': 240 obs. of 1 variable:

Formal class 'sample_data' [package "phyloseq"] with 4 slots

..@ .Data :List of 1

.. ..$ : int 56 50 34 64 66 54 65 76 70 84 ...

..@ names : chr "AGE"

..@ row.names: chr "CRC.109" "CRC.236" "CRC.201" "CRC.96" ...

..@ .S3Class : chr "data.frame"

dput(head(AGE.data, 10))

new("sample_data"

, .Data = list(c(56L, 50L, 34L, 64L, 66L, 54L, 65L, 76L, 70L, 84L))

, names = "AGE"

, row.names = c("AGE.109", "AGE.236", "AGE.201", "AGE.96", "AGE.21", "AGE.56",

               "AGE.64", "AGE.225", "AGE.24", "AGE.57")

, .S3Class = "data.frame"

)

1
Please check the str(AGE.data). Also, it is not clear whether this is a data.table, or table or data.frame - akrun
@akrun I edited my post to include the output of str(AGE.data). - Brian Lee
Could you dput the output i.e. dput(head(AGE.data, 10)). Seems like it is a list output. - akrun
@akrun I've edited my post to include it - Brian Lee
It is giving me errors. I don't have the package phyloseq installed. Hopefully, someone will respond. - akrun

1 Answers

0
votes

The original poster solved the issue by using matrix(as.numeric(unlist(AGE.data)),nrow=nrow(AGE.data))