r - Error - replacement has [x] rows, data has [y] -


i have numeric column ("value") in dataframe ("df"), , generate new column ("valuebin") based on "value." have following conditional code define df$valuebin:

df$valuebin[which(df$value<=250)] <- "<=250" df$valuebin[which(df$value>250 & df$value<=500)] <- "250-500" df$valuebin[which(df$value>500 & df$value<=1000)] <- "500-1,000" df$valuebin[which(df$value>1000 & df$value<=2000)] <- "1,000 - 2,000" df$valuebin[which(df$value>2000)] <- ">2,000" 

i'm getting following error:

"error in $<-.data.frame(*tmp*, "valuebin", value = c(na, na, na, : replacement has 6530 rows, data has 6532"

every element of df$value should fit 1 of which() statements. there no missing values in df$value. although if run first conditional statement (<=250), exact same error, "...replacement has 6530 rows..." although there way fewer 6530 records value<=250, , value never na.

this link notes similar error when using aggregate() bug, recommends installing version of r have. plus bug report says fixed. r aggregate error: "replacement has <foo> rows, data has <bar>"

this link seems more related issue, , issue here issue his/her conditional logic caused fewer elements of replacement array generated. guess must issue well, , figured @ first must have "<=" instead of "<" or vice versa, after checking i'm pretty sure they're correct cover every value of "value" without overlaps. r error in '[<-.data.frame'... replacement has # items, need #

you use cut

 df$valuebin <- cut(df$value, c(-inf, 250, 500, 1000, 2000, inf),      labels=c('<=250', '250-500', '500-1,000', '1,000-2,000', '>2,000')) 

data

 set.seed(24)  df <- data.frame(value= sample(0:2500, 100, replace=true)) 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -