-2

I need help with some coding! It would be great if some of you could help me...

I wanted to ask if it's possible to create a loop in order to sequentially create several new variables (names ranging from A to Z, for example) which are equal to a group of observations which is derived from a combination of specific values from the other variables (for example, if you have two other variables which are called 'woman' (1 if yes, 0 if no) and 'rent' ('high','medium' and 'low'), what I wanted to ask is if it is possible to form a loop to create variables which are equal to every combination of values from these groups of observations from the other variables.

Here is an example, but without a loop:

a<-data[data$woman==1 & data$rent=="high",]
b<-data[data$woman==1 & data$rent=="medium",]

If there were to be more variables other than 'woman' and 'rent', how could this be coded?

I'm really thankful if someone has the time to help me!

  • If you expect someone to give an answer, please provide data using `dput()`, and have a look here: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Pax May 27 '22 at 18:20
  • Or with `dplyr`, `data %>% group_by(woman, rent) %>% group_split()` – Gregor Thomas May 27 '22 at 18:56
  • And, for a good read on why you should want the result in a `list` instead of sequentially named variables, see my answer on [Lists of data frames](https://stackoverflow.com/a/24376207/903061). – Gregor Thomas May 27 '22 at 18:57

0 Answers0