-1

This is my second time posting, so I hope I get the "reproducible example" thing right.

Here is the error,

Error in if ((sound[i, 3] < -10 & sound[min(81, i + 1), 3] > -10) | (sound[i,  : 
  missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In Ops.factor(sound[i, 3], -10) : < not meaningful for factors
2: In Ops.factor(sound[min(81, i + 1), 3], -10) :
  > not meaningful for factors
3: In Ops.factor(sound[i, 3], -10) : > not meaningful for factors
4: In Ops.factor(sound[min(81, i + 1), 3], -10) :
  < not meaningful for factors

Here is the code,

> for(i in 3:length(sound[,2])){
+ if(sound[i,3]==-10){
+ soundout[m,7]=sound[i,2]
+ m=m+1}
+ if((sound[i,3]< -10&sound[min(81,i+1),3]>-10)|(sound[i,3]>-10&sound[min(81,i+1),3]< -10)){
+ soundout[m,7]=(10-sound[i,3])*(sound[i+1,2]-sound[i,2])/(sound[i+1,3]-sound[i,3])+sound[i,2]
+ m=m+1}}

Can someone please tell me how to make the factor sound[1,3] a numeric value? Thank you,

Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112
Dent
  • 3
  • 3
  • The problem is that sound[i, 3] is a factor. – Señor O Jun 20 '14 at 18:32
  • The first part of the top answer will help you: http://stackoverflow.com/questions/6979625/arithmetic-operations-on-r-factors/6980780#6980780 – Señor O Jun 20 '14 at 18:34
  • Sorry, what is a factor? Are you saying that even though the second and third columns of sound consist of number the values given for sound[i, 3] are string values of the numbers? – Dent Jun 20 '14 at 18:42
  • I see now. How do use the value as numeric? From your link, I thought as.numeric(sound[i,3]) would do it, but it doesn't. – Dent Jun 20 '14 at 18:53
  • Well, if you didn't expect them to be factors, there may be a problem reading your data in. Perhaps there's a non-numeric value in there somewhere. You will want to make sure you are reading in clean data. – MrFlick Jun 20 '14 at 19:07
  • @Dent nowhere in the link does it say `as.numeric` works on factors - it only works on characters. So you have to do `as.numeric(as.character(sound))` – Señor O Jun 20 '14 at 21:43

1 Answers1

2

For the example to be reproducible, we need both code and data. Right now, I can't take your code and run it on my machine and generate the same error you see (which is what is meant by having a reproducible example). You don't need to give us your entire dataset, just a similar simple example with 5-10 rows and only the necessary columns so that we can see what's going on.

Senor O is absolutely right that the problem is that sound[, 3] is a factor. Before running the code you posted, run this:

colnames(sound) <- sound[1, ]
sound <- sound[-1:2, ]

sound[, 2] <- as.numeric(as.character(sound[, 2]))
sound[, 3] <- as.numeric(as.character(sound[, 3]))
sound[, 7] <- as.numeric(as.character(sound[, 7]))

That will turn the data you want to do arithmetic operations on into numbers. It seems like you have headers or data you don't want to use in rows 1 and 2, so I only coerced rows three and on for the columns that you use in your loop.

Edit: MrFlick is also right that it's likely possible to fix this up-front when you read the data, but without knowing what kind of file you're reading and how you're doing that we can't necessarily say what you should do. If you're reading something like a CSV file from disk, though, set as.is = TRUE and that will bring in numbers as numeric, characters as characters, etc.

Ajar
  • 1,786
  • 2
  • 15
  • 23
  • Sorry. I wrote a .txt to sound via read.table. Here is the first three columns and 10 rows. V1 V2 V3 1 PRES HGHT TEMP 2 hPa m C 3 1025.0 8.6 4 1019.0 61 9.0 5 1008.0 152 15.0 6 1000.0 220 16.2 7 993.0 280 16.4 8 990.0 305 16.2 9 954.9 610 13.6 10 925.0 879 11.4 – Dent Jun 20 '14 at 19:35
  • Okay, there are a couple of things going on here. First, though, my above code should work on your existing data, given what you've posted. Does it? – Ajar Jun 20 '14 at 20:43
  • 1
    `as.numeric` turns factors into their integer codes – Señor O Jun 20 '14 at 21:44
  • 1
    Also, you cannot mix classes within columns, so it will get converted back into factor – Señor O Jun 20 '14 at 21:45
  • 1
    Oops, you're right, it's `as.numeric(as.character(factor))`. To get numbers he's going to have to strip the headers and make them column names. Edited accordingly. – Ajar Jun 20 '14 at 22:01