I've imported a spreadsheet of data into R using the read.xlsx command but there are elements marked Low interspersed in the data because they weren't measured properly.

So an example column in the data looks like this:

[15, 6, Low, 23, 39, Low]

I want to replace the Low elements with numeric values but when I try a command like:

data[3,2] <- 4 #.....I get the following error message:

Warning message: 
In '[<-.factor '('*tmp*', iseq, value = 4): invalid factor level, NAs generated.

I've tried various combinations of as.numeric, as.character, as.factor but none of them get round this error message.

I've been told that the way to do this, is to convert the Low elements to missing values but when I do this and try to replace the NAs with a numeric, I still get the same error message!

有帮助吗?

解决方案

A factor is a type of variable that only accepts predefined values. So if you have the following factor :

fac <- factor(c("15","6","Low","23","39","Low"))

Then the predefinded set of values is :

R> levels(fac)
[1] "15"  "23"  "39"  "6"   "Low"

If you want to attribute "4" as a velue, you either have to change your factor labels :

R> factor(fac, levels=c("15","23","39","6","Low"), labels=c("15","23","39","6","4"))
[1] 15 6  4  23 39 4 

Or first convert it to a character vector :

R> v <- as.character(fac)
R> v[v=="Low"] <- "4"
R> v
[1] "15" "6"  "4"  "23" "39" "4" 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top