Search code examples
rvariablesassign

R programming: Assign a columns but only with specific values in another column


From my dataset 'DataHP2' I assigned columns 8,9,10,14,15,16,18,19 and 20 to variable 'd' So far soo good!

d <- DataHP2[c(8:10,14:16,18:20)]

Now I want to create another variable 'dl' that assigns the same columns BUT only for those values of these columns that ALSO have a valye of '1' in column 12. How am I going to code this?


Solution

  • you can also do it with which() function

    dl <- DataHP2[which(DataHP2[12] == 1),c(8:10,14:16,18:20)]