-2

as a newbie in R, I got stuck to this issue of data manipulation: I need to create a factor with 4 levels from to two numerical vectors (enable, restrict). The latter are factor scores.

The 4 levels should be:

  • level 1 = data$enable > median(data$enable) & data$restrict > median(data$restrict)

  • level 2 = data$enable < median (data$enable) & data$restrict > median (data$restrict)

and the following two more combinations. I want to label the levels as well. I am not sure how to nest it in the factor() function.

Any help? Thank you

gav2016
  • 25
  • 5
  • could you share an example of your data to work with – mtoto Jan 17 '16 at 13:59
  • What do you need to know? – gav2016 Jan 17 '16 at 14:06
  • everything that's described [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – mtoto Jan 17 '16 at 14:07
  • You could use `ifelse` – akrun Jan 17 '16 at 14:10
  • Data are confidential, I can share a data frame with the two variables that needs recoding into to a factor. Would that work? – gav2016 Jan 17 '16 at 14:19
  • Try `with(data, ifelse(enable > median(enable) & restrict > median(restrict), 1, ifelse(enable < median(enable) & restrict > median(restrict), 2, ifelse(enable > median(enable) & restrict < median(restrict), 3, 4))))` and then wrap it with `factor` if necessary. – akrun Jan 17 '16 at 14:22
  • I have got this error Error in ifelse((enabling < median(enabling) & restrictive < median(restrictive, : argument "yes" is missing, with no default – gav2016 Jan 17 '16 at 14:38
  • Using style <- factor(with(data, ifelse(enabling > median(enabling) & restrictive > median(restrictive), 1, ifelse(enabling < median(enabling) & restrictive > median(restrictive), 2, ifelse(enabling > median(enabling) & restrictive < median(restrictive), 3, ifelse((enabling < median(enabling) & restrictive < median(restrictive, 4))))) – gav2016 Jan 17 '16 at 14:39
  • You haven't given any example. Based on the example I provided below, it works. – akrun Jan 17 '16 at 14:39

1 Answers1

1

One option would be using nested ifelse

with(data, 
   ifelse(enable > median(enable)  & restrict  > median(restrict),1, 
   ifelse(enable < median(enable) & restrict > median(restrict), 2, 
   ifelse(enable > median(enable) & restrict < median(restrict), 3,
      4))))
#[1] 2 4 1 2 3 1 4 3 1 4

Or another option would be to get the colMedians (from library(matrixStats)) of the dataset, check whether the data is greater than the column medians, add 1 to the logical index to convert it to a 1/2 values, paste the columns together, convert to factor and then if needed can be coerced to numeric.

library(matrixStats)
factor(do.call(paste0,  as.data.frame((data >
      colMedians(as.matrix(data))[col(data)])+1L)))

data

set.seed(24)
data <- data.frame(enable = sample(20, 10, replace=TRUE),
           restrict = sample(20, 10, replace=TRUE))
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662