0

studio and I have a data frame with columns a and b.

 a        b
 EUR      1
 SGD      2
 AUD      3
 CAD      4
 JPY      5
 HKD      6

Right now the data only has 6 rows in total, but in the future, it can look like this.

a        b
 EUR      1
 SGD      2
 AUD      3
 CAD      4
 JPY      5
 HKD      6
 NZD      7

Because the data is imported with a new file every day, so I want to automate and only choose column a with "EUR", "USD","NZD". One of my problem is for the upcoming data frame I don't know if there gonna be NZD or EUR or USD, so I want R to scan it and if there is corresponding value. R will choose it Does anyone know how to do it?

  • 1
    Of course. And welcome to stack overflow. Can you please provide a reproducible example https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Georgery Dec 17 '19 at 13:37
  • Hi georgery, I have not made any code yet. I can perform filter function in dplyr, but I am not sure how to automate it in the future – bach_19971010 Dec 17 '19 at 13:42
  • What does it mean "*automate*"? – David Arenburg Dec 17 '19 at 13:45
  • Hey, dplyr helps you - with a lot of things. Here is a super good book that will help you get started with R: https://r4ds.had.co.nz/ It's available online and it's well written. - Give it a shot. ;) – Georgery Dec 17 '19 at 13:51

3 Answers3

1

This is the base-R version

df <- data.frame(
    a = c("EUR", "SGD", "AUD", "CAD", "JPY", "HKD")
    , b = c(1  ,   2  ,   3  ,   4  ,   5  ,   6  )
)


df[df$a %in% c("EUR", "USD", "NZD"), ]
Georgery
  • 7,643
  • 1
  • 19
  • 52
1

You can easily go the dplyr way:

yourdf %>%
    filter(a %in% c("EUR", "USD", "NZD"))
anddt
  • 1,589
  • 1
  • 9
  • 26
-1

And this is the dplyr version.

library(dplyr)

df <- data.frame(
    a = c("EUR", "SGD", "AUD", "CAD", "JPY", "HKD")
    , b = c(1  ,   2  ,   3  ,   4  ,   5  ,   6  )
)

df %>%
    filter(a %in% c("EUR", "USD", "NZD"))
Georgery
  • 7,643
  • 1
  • 19
  • 52
  • Which is the difference from @Andrea Dodet answer? – s__ Dec 17 '19 at 13:53
  • I don't get it. Why is my answer voted down here? – Georgery Dec 17 '19 at 13:53
  • You get downvote (in this case, mine) because it's equal to another answer, and also you can edit your answer above, rather than add another anwer. – s__ Dec 17 '19 at 13:55
  • I didn't see the other answer, while I was writing mine - I think this is still no reason to vote it down, but if you want to know the difference: this answer is reproducible - the other one is not. – Georgery Dec 17 '19 at 13:55
  • I don't want to edit my other answer, because it's a different one. – Georgery Dec 17 '19 at 13:57
  • You can delete an answer, if you've been "slower" than another user. To me it happens frequently to trash an answer before posting it, there are out here a lot of fast&good guns. In case, you can edit your first answer. There are a lot of answer with multiple options ([also famous ones](https://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format) ). – s__ Dec 17 '19 at 14:04