Apparently, the trivial task of selecting rows in a data frame and then ordering them eludes me and at the same time drives me crazy. For example, let it have a trivial data frame:
country = c("US", "US", "CA", "US") company = c("Apple", "Google", "RIM", "MS") vals = c(100, 70, 50, 90) df <- data.frame(country, company, vals)
Lets arrange it with vals
> df[order(vals),] country company vals 3 CA RIM 50 2 US Google 70 4 US MS 90 1 US Apple 100
Works great. Now try to select only American companies and order values ββthere. We get some fictitious result.
> df[country=="US", ][order(vals),] country company vals 4 US MS 90 2 US Google 70 NA <NA> <NA> NA 1 US Apple 100
Lets you complete the order, and then select. Again, a dummy result
> df[order(vals),][country=="US", ] country company vals 3 CA RIM 50 2 US Google 70 1 US Apple 100
How do I get a data frame that includes only US companies and is sorted by val?