I have a numeric dataframe read from a csv
file in this way:
df <- read.csv(path, sep=",", row.names = 1, header=TRUE)
So the first line is used as header and the first column is the index. I want to do something like the python
function df.values
, namely returns a matrix with only the numeric values.
I have tried
X = matrix(df)
X = data.matrix(df)
But none of them returns what I want i.e for instance I am not able to get the first numeric value by calling X[1,1]
.
To clarify, I have created a data frame as follows:
Firstly, I have created the matrix with
X = matrix(data=c(1,2,3,4,5,6), nrow=3, ncol=2)
Then the data.frame
by using
df_dummy = data.frame(X, row.names = c("A","B","C"))
And what I get with the two mentioned method is:
> X = matrix(df_dummy)
> X[1,1]
[[1]]
[1] 1 2 3
And
> X = data.matrix(df_dummy)
> X[1,1]
[1] 1
> X
X1 X2
A 1 4
B 2 5
C 3 6
So X
is not only numerics, it still has the labels.
What I would like to have is what I built at the beginning, namely:
X
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
A walk around would be to create an empty matrix and fill in with a for loop, but it would be great to avoid this and find something more efficient.
Thank you for any answer, I am trying to learn R
from scratch using my knowledge in python
!
CodePudding user response:
Do you mean by?
data.matrix(sapply(df, as.numeric))
Without column names:
unname(data.matrix(sapply(df, as.numeric)))