I have a data.frame with a column Material that has 7 different codes for 7 different materials: 'AC', 'GGIJ', 'HPE', 'ONB', 'PE', 'PVC', 'ST'

Is there a way to stratify my data considering two or more of these as equivalent (i.e. generating 6 levels instead of 7 because for example 'PVC' is considered in the same level as 'PE')

Specifically I'm using this for survival analysis with the survive package. Trying to have more control on the survdiff(formula = survObj ~ Material, data = data) and survfit(survObj ~ Material., data = data) functions.

However, I see this also being useful for visualization or other types of data analysis.

I am aware I can do this by adding a column in the data.frame, but since I need to try different combinations, I'd love to do it on-the-fly.


closed as off-topic by Scortchi - Reinstate Monica May 24 '17 at 14:25

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – Scortchi - Reinstate Monica
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ I am not really sure what kind of advice your are expecting. Nevertheless, I'd like to mention the R package forcats that has functions for collapsing factor levels. $\endgroup$ – hplieninger May 24 '17 at 10:17
  • 1
    $\begingroup$ @hplieninger, sorry if my question ins't clear. However, the package you suggests seems to have the functionality I'm looking for! Thanks $\endgroup$ – ltr May 24 '17 at 10:39
  • $\begingroup$ Then, I add this as an answer, feel free to "accept" it. $\endgroup$ – hplieninger May 24 '17 at 14:12
  • 1
    $\begingroup$ This is a quesiton for StackOverflow. $\endgroup$ – David Arenburg May 24 '17 at 14:22
  • $\begingroup$ @DavidArenburg, you are very right, I just realized it too late. Apologies. $\endgroup$ – ltr May 25 '17 at 9:15

The R package forcats has functions for collapsing factor levels, for example, fct_collapse():

> tmp1 <- factor(letters[1:4])
> tmp1
[1] a b c d
Levels: a b c d
> forcats::fct_collapse(tmp1, x = c("a", "b", "c"))
[1] x x x d
Levels: x d

Not the answer you're looking for? Browse other questions tagged or ask your own question.