mercredi 25 septembre 2019

Creating a new list from an old list (R)

As a python user, R has been somewhat intuitive thus far. However, I'm running into a problem with creating a new list from an old one.

Specifically I have a list:

> x
[[1]]
  [1] "Heads" "Tails" "Tails" "Heads" "Tails" "Heads"
  [7] "Tails" "Tails" "Tails" "Heads" "Heads" "Tails"
  ... .... ... etc.

This is length 100. I'd like to loop through this list and create a list y with the values 'Heads' = 1, 'Tails' = -1. My attempt so far:

> for (val in x) {
         ifelse(val == 'Heads', c(y,1), c(y,-1))}

Which appends an empty list y. However, the result is a list of length 2. Can anybody help me here?

Aucun commentaire:

Enregistrer un commentaire