Assignment to data.frame causes an unwanted type change in R
Say I generate some data like so:
dat <- data.frame(x = rnorm(100), y = rnorm(100), z = rnorm(100))
dat[sample(nrow(dat), 5), 3] <- NaN
dat[sample(nrow(dat), 5), 3] <- Inf
Now, some of the z-values are Inf
or NaN
.
The first 10 rows of the results from
cut(dat$z[is.finite(dat$z)],6)[1:10]
are
开发者_JAVA百科 [1] (0.286,1.17] (0.286,1.17] (0.286,1.17] (0.286,1.17] (0.286,1.17]
[6] (0.286,1.17] (-1.48,-0.599] (-1.48,-0.599] (-0.599,0.286] (0.286,1.17]
6 Levels: (-2.37,-1.48] (-1.48,-0.599] (-0.599,0.286] ... (2.06,2.94]
but if I try to make the following assignment
dat$col[is.finite(dat$z)] <- cut(dat$z[is.finite(dat$z)],6)
I get integers instead of the labels:
> dat$col[1:10]
[1] 4 4 4 4 4 4 2 2 NA 3
How do I assign the factor labels correctly just to the subset of rows?
Thanks! Uri
dat[is.finite(dat$z),"col"] <- cut(dat$z[is.finite(dat$z)],6)
Should work. I don't know why assigning via $
doesn't, though.
I am not totally sure that my answer is what you want, but if you want the labels instead of the integers representing the factor try as.character
:
dat$col[is.finite(dat$z)] <- as.character(cut(dat$z[is.finite(dat$z)],6))
if you want it to be a factor instead of a character vector, wrap this into a call to factor:
dat$col[is.finite(dat$z)] <- factor(as.character(cut(dat$z[is.finite(dat$z)],6)))
精彩评论