0

Use the for loop and if control statements to list the women's names, age 34 or more that emabrked from S (Southampton), on the Titanic.

for (i in 1:length(titanicDataset$age))
{if (!is.na(titanicDataset$age[i]) & (titanicDataset$age[i] >= 34) & (titanicDataset$sex==female) & (titanicDataset$home.dist==Southampton))
{print(titanicDataset$name[i])}}

Error in female : object 'female' not found

[enter image description here][1]


  [1]: https://i.stack.imgur.com/l857a.jpg
Z J
  • 1
  • 1
  • 3
    This is not reproducible: we don't have `titanicDataset`, and can only infer the values of the `female` and `Southampton` *objects* (are they really supposed to be strings?). This does not need to be a `for` loop, though; if I'm inferring correctly, then `subset(titanicDataset, !is.na(age) & age <= 34 & sex == female & home.dist == Southampton)$name`. (More notably, R often does *vectorized* work much more efficiently and/or more-readable than literal `for` loops.) – r2evans May 25 '21 at 15:33
  • Welcome to SO, ZJ! Your first question is not bad in that it gives sample code, thanks for that! It is not really reproducible, though, for the reasons I stated above. If my comment doesn't resolve your question, though, please [edit] your question and add sample data by pasting the output from `dput(x)`, where `x` is a sufficient sample of `titanicDataset` that is not huge, but still shows some rows that meet your condition and some that do not; how you choose those rows does not matter. Also, make sure to provide your objects `female`/`Southampton`. – r2evans May 25 '21 at 15:35
  • A few good references for keeping questions fully *reproducible*: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. Thanks! – r2evans May 25 '21 at 15:36
  • (BTW: from a *functional* perspective, this function is not in good form: first, is accessing `titanicDataset` in a scope-breach, which R is forgiving with, but can make some functions more difficult to troubleshoot. It is generally preferred that any variable used inside a function be explicitly passed to it as an argument. Additionally, in this case, it's unclear what your `name` argument is used for. Perhaps it's a hold-over from an unrelated purpose of your function?) – r2evans May 25 '21 at 15:37
  • I think the problem is that you cannot parse the variable `name` to $ in this way `print(titanicDataset$name[i])`. – Pete Kittinun May 25 '21 at 15:41
  • Perhaps try `print(titanicDataset[i, name]` with name being a character-vector. – Martin Gal May 25 '21 at 18:05
  • I think you should change female to string "female" – demonplus May 26 '21 at 19:26
  • now im getting the below argument error for (i in 1:nrow(titanicDataset)) {if ((titanicDataset$age >='34') & (titanicDataset$sex=='female') & (titanicDataset$home.dist=='Southampton')) {print(titanicDataset$name)}} Error in if ((titanicDataset$age >= "34") & (titanicDataset$sex == "female") & : argument is of length zero > – Z J May 26 '21 at 19:40

0 Answers0