Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List object cannot be coerced to type 'double [closed]

Tags:

r

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 ?

like image 699
was Avatar asked Feb 26 '26 02:02

was


1 Answers

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.

like image 155
Señor O Avatar answered Feb 28 '26 17:02

Señor O



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!