I have a data.frame ("data.csv") of 93 x 28 I want to convert into matrix for further plotting, it looks like this:
SYMBOL BT.20 CAL.51 MDA.MB.436 BT.549
A 3.039 4.908 3.865 3.818
B 4.349 5.399 6.071 5.313
C 7.509 8.091 6.48 6.660
D 3.429 4.394 3.622 3.873
E 3.369 6.716 3.557 3.346
the code
data <- read_csv("data.csv")
rnames <- data[,1] # assign labels in column 1 to "rnames"
mat_data <- data.matrix(data[,2:28]) # transform column 2 - end into a matrix
rownames(mat_data) <- rnames # assign row names
produces
ERROR: Error in `rownames<-`(`*tmp*`, value = list(SYMBOL = c("A", "B", :
length of 'dimnames' [1] not equal to array extent
column 1 doesn't have duplicate names or missing values
edit: with read.csv()
instead, it works just fine
The error is saying that the value being assigned to the rownames is a list and the "length" of a list with a single vector in it is 1, rather than the length of the vector. I'm not able to say why that should be happening since the usual behavior is for the "[" function to "drop" single columns into atomic vectors. Like you, I would have expected rnames
to be an atomic vector of length 39, but it appears you need to use this instead:
rownames(mat_data) <- unlist( rnames ) # assign row names
Rich and Apom have better eyes than I do. The read_csv function is producing an object with a different class and associated extraction function than would have read.csv
. Since you are in the "hadleyverse", you therefore should use this:
rnames <- data[[1]]
The tibble
-classed objects have a different version of the [
-function than do data.frames. The [[
-function appears to act the same for both classes.
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.