I use R and when I try to run the following code:
f = scan(file="c:\\myfile.txt", sep=' ', what=list('numeric'))
MY_MATRIX.M = matrix(f, ncol = 4, byrow = TRUE)
Var.names = c('column1','column2', 'column3', 'column4')
colnames(MY_MATRIX.M) = Var.names
TEST = data.frame(MY_MATRIX.M)
t1 = TEST[which(TEST[,4] == 0, arr.ind = TRUE),]
t2 = TEST[which(TEST[,4] == 1, arr.ind = TRUE),]
I have the following error:
Error in which(TEST[,4] == 0, arr.ind = TRUE :
(list) object cannot be coerced to type 'double'
My file has 4 columns and consist of numbers like an array. Can anyone give me a hand on solving the problem ?
The problem you're experiencing is due to the argument what=list(numeric). You want to tell R that the file has just numeric values, not that it contains lists with numeric elements. So just use what=numeric, or don't use what at all.
I don't import text files often, but when I do, I prefer read.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With