简体   繁体   中英

How to create a data.frame with a matrix as a variable in R?

My data frame has a first column of factors, and all the other columns are numeric.

       Origin spectrum_740.0 spectrum_741.0 spectrum_742.0   etc....
1     Warthog      0.6516295      0.6520196      0.6523843
2       Tiger      0.4184067      0.4183569      0.4183805
3 Sperm whale      0.9028763      0.9031688      0.9034069

I would like to convert the data frame into two variables, a vector (the first column) and a matrix (all the numeric columns), so that I can do calculations on the matrix, such as applying msc from the pls package. Basically, I want the data frame to be like the gasoline data set from pls , which has one variable as a numeric vector, and a second variable called NIR as a matrix with 401 columns.

Alternatively, if you have any suggestions for applying calculations to the numeric data while keeping the Origin column connected, that would work too, but all the examples I have seen use gasoline or similarly formatted data frames to do the calculations on the NIR matrix.

Thank you!

M = as.matrix(df[,-1])
row.names(M) = df[,1]
M
            spectrum_740.0 spectrum_741.0 spectrum_742.0
Warthog          0.6516295      0.6520196      0.6523843
Tiger            0.4184067      0.4183569      0.4183805
Sperm_whale      0.9028763      0.9031688      0.9034069

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