[datatable-help] creating levels for a variable
carlsutton
suttoncarl at ymail.com
Sat Dec 26 23:33:48 CET 2015
Please forgive me for asking such a basic question. I have been fumbling
around trying to learn R for a couple years via Data Camp, Coursera, and
Lynda.com but keep amazing myself at how inadequate I am.
I have searched this site and stack overflow and have not found an answer,
probably because the question is so basic.
I have some rather large data tables. In performing my exploration work it
would be helpful to combine the values of one variable into separate ranges,
then explore the relationship of those ranges to other variables.
I have created a very simple example of what I am attempting to do, and cut
and pasted the resulting error messages.
The goal is to populate the variable levels with first, second or third,
depending on the value of variable "b".
# create a range variable
set.seed = 1
library(data.table)
dt <- data.table(a=1:10, b = sample(c(200:50000),10), c = 21:30, level =
1:10)
dt
dt1 <- dt[, .(':=' (if (dt$b < 12000) level = "first",
if (dt$b >= 12000 & dt$b < 34000) level = "second",
if (dt$b > 34000) level = "third"))]
# gives error Error in `:=`(if (dt$b < 12000) level = "first", if (dt$b >=
12000 & dt$b <
# : Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are
defined for use in j,
# once only and in particular ways. See help(":=").
dt1
dt1 <- dt[, lapply(if (dt$b < 12000) level = "first",
if (dt$b >= 12000 & dt$b < 34000) level = "second",
if (dt$b > 34000) level = "third")]
# gives error Error in match.fun(FUN) :
# 'if (dt$b >= 12000 & dt$b < 34000) level = "second"' is not a function,
character or symbol
# In addition: Warning message: In if (dt$b >= 12000 & dt$b < 34000) level
= "second" :
# the condition has length > 1 and only the first element will be used
dt1
any help is appreciated.
Carl Sutton
-----
Carl Sutton
--
View this message in context: http://r.789695.n4.nabble.com/creating-levels-for-a-variable-tp4715934.html
Sent from the datatable-help mailing list archive at Nabble.com.
More information about the datatable-help
mailing list