0

The basic assignment was to create 2 columns (age & id) and figure out the id number of those old enough to drink. My professor said everything was correct except the bottom. I can find the ages of the people who can drink, but how can I determine and transmit their corresponding id number?

Thank you in advance.

id <- c(seq(1,30, by = 1)) #column id

age <- sample(12:60,30, replace = T) #column age

age_id <- cbind(age, id) #matrix of column id and column age
age_id

# NEED TO CORRECT THIS PART
ok_drink <- age_id
ok_drink [(age>20) & (id>20)]
Frank
  • 66,179
  • 8
  • 96
  • 180
AJ19
  • 83
  • 7
  • 1
    Your last line is missing a closing `]` and says: "anyone over 20 and with an id over 20". Instead, you want to see the id of anyone whos age is over 20... Also, please get in the habit of using `data.frame` rather than `cbind` to create a `data.frame`. There are many tutorials on how to subset in R... I'd suggest spending some time there first. – Justin Feb 04 '14 at 21:35
  • My error on the closing bracket. Thanks for the tip on using data.frame, however we were instructed to use cbind for this assignment. – AJ19 Feb 04 '14 at 21:50
  • Whoever is suggesting you use `cbind` is talking out of their hat. There is nothing necessarily wrong with using a `matrix`, but it just adds a needless complexity to this subsetting task. `cbind` will also come back to bite you should you start wanting to join two types of data (e.g. - strings and numbers), as the data will all be forced ('coerced') to one type. Which is not what you want typically. – thelatemail Feb 04 '14 at 22:58

2 Answers2

1

The sub-matrix with elements where age > 20:

age_id[age_id[,'age'] > 20,]

Just the ids of these elements:

age_id[age_id[,'age'] > 20, 'id']

The sub-matrix with elements where age > 20 and id > 20:

age_id[age_id[,'age'] > 20 & age_id[,'id'] > 20,]

Thanks to the free variables age and id, you could write simpler, but as @thelatemail pointed out in comments, that could lead to confusions and frustration later. But here they are anyway, the simpler equivalents of the above, use at your own risk:

age_id[age > 20,]
age_id[age > 20, 'id']
age_id[age > 20 & id > 20,]
Community
  • 1
  • 1
janos
  • 120,954
  • 29
  • 226
  • 236
  • 1
    I don't think is actually a good answer. You are subsetting the matrix `age_id` on the basis of the free variables `age` and `id`. This could lead to all sorts of stuff-ups if you sort the matrix or change the free variables independently of the matrix. – thelatemail Feb 04 '14 at 22:40
  • @thelatemail you're right, thanks for the comment. I added alternatives without using the free variables. Better? – janos Feb 04 '14 at 22:53
  • I'd just dump the free variable parts altogether as they are potentially misleading. – thelatemail Feb 04 '14 at 22:58
  • @thelatemail ok I rewrote again, thanks for the comment! – janos Feb 04 '14 at 23:02
0

Although it is homework you have shown effort. So, here is answer..but may be you can browse through more...

ok_drink <- age_id[age_id[,1] > 20,"id"] # just id
ok_drink <- age_id[age_id[,1] > 20,]    #all data with age>20
Ananta
  • 3,671
  • 3
  • 22
  • 26
  • Thanks Ananta. I tried using the $ earlier, and it says I cannot use it for atomic vectors. Do you have any other suggestions? Also, you have a closed parenthesis, but not an open one. – AJ19 Feb 04 '14 at 21:51
  • yes, it is matrix, so either you have to change to `data.frame` or just use the names without `$` – Ananta Feb 04 '14 at 22:02
  • This answer has the same problem as the other. You are referencing the vector `age` in the global environment for subsetting, not `age_id$age`. You could however employ `with` like: `with(age_id, age_id[age > 20 & id > 20,] )` to simplify things a little. – thelatemail Feb 04 '14 at 23:03
  • `with` did not allow; probably because it was matrix? So, i think to get rid of global variable, variable by position may be best way, if OP does not want to change it to data frame. edited above to address that – Ananta Feb 04 '14 at 23:16