Data frame challenge [🇷 for BE/BA]
Hi all,
I am struggling with import of a data from a file having a structure like:
Now, I would like R to understand that
1. we are not dealing with factors, the data frame is a series of numeric vectors.
2. Whenever we encounter something non-numeric such as Apple and Banana we will set it to zero.
So I have this solution:
A is now effectively a matrix, or we can
And although it works, it is painfully unreadable and it also throws warnings (coercion).
I am looking for a solution that is much more readable and not throwing warnings (and we are not going to discuss suppression of warnings, please, it sends shivers down my spine to do it). Regarding readability: I am fine with a solution that takes more coding/more lines, so this is not a competition in doing a lot of stuff in a single line. Single line = more elegance, to some = less readbility to a simpleton like me.
Can anyone help here?
Many thanks.
I am struggling with import of a data from a file having a structure like:
Now, I would like R to understand that
1. we are not dealing with factors, the data frame is a series of numeric vectors.
2. Whenever we encounter something non-numeric such as Apple and Banana we will set it to zero.
So I have this solution:
A=read.table("SomeFile.csv", header=T)
for (i in 1:ncol(A))
{
A[,i]=as.numeric(as.character(A[,i]))
A[,i][is.na(A[,i])]=0
}
A is now effectively a matrix, or we can
as.matrix
or matrix
it directly. And although it works, it is painfully unreadable and it also throws warnings (coercion).
I am looking for a solution that is much more readable and not throwing warnings (and we are not going to discuss suppression of warnings, please, it sends shivers down my spine to do it). Regarding readability: I am fine with a solution that takes more coding/more lines, so this is not a competition in doing a lot of stuff in a single line. Single line = more elegance, to some = less readbility to a simpleton like me.
Can anyone help here?
Many thanks.
—
Pass or fail!
ElMaestro
Pass or fail!
ElMaestro
Complete thread:
- Data frame challengeElMaestro 2016-12-17 19:40
- Data frame challenge mittyri 2016-12-18 01:41
- Data frame challenge ElMaestro 2016-12-18 11:47
- Data frame challenge wligtenberg 2016-12-19 14:29
- Data frame challenge zizou 2016-12-19 18:41
- Data frame challenge ElMaestro 2016-12-19 21:06
- Data frame challenge mittyri 2016-12-18 01:41