How to formulate a statement in r? -
so question: have been given data set , instruction are:
growth patterns generated transforming x variable new categorical variable, can named "growth". first category assigned islands in x variable between 15 50.
so question, main headache how write "between 15 50 in r language. have
growth$mediumgrowth.islands <- growth$sasiaurban.x[growth$sasiaurban.x ???] but not know command put in ??? part.
let's reproducible:
set.seed(47) df <- data.frame(x = sample(60, 100, replace = true)) your exact questions, how write "between 15 , 50" answered telling r "greater 15 and less 50":
## what's between 15 , 50? df$x > 15 & df$x <= 50 that gives vector of true/false. it's same length x , true when criteria match (between 15 , 50), , false otherwise. (nb: used greater 15 , less or equal 50, might want adjust that.) filling "???" growth$sasiaurban.x[growth$sasiaurban.x > 15 & growth$sasiaurban.x <= 50].
## assign new column df$between15and50 <- df$x > 15 & df$x <= 50 if want more categories, makes sense them @ once, , cut makes easy:
## or use cut cut(df$x, breaks = c(0, 15, 50, 100)) df$category <- cut(df$x, breaks = c(0, 15, 50, 100)) ## adding labels instead df$category2 <- cut(df$x, breaks = c(0, 15, 50, 100), labels = c("low", "medium", "high")) head(df)
Comments
Post a Comment