mattakuu
mattakuu

Reputation: 13

Boxplot for one element instead of all of them

install.packages("HSAUR")
data("Forbes2000", package="HSAUR")  
boxplot((log(Forbes2000$marketvalue)~Forbes2000$country))

I get the boxplot for each and every country, is there a way for me to get it for only one desired country? For example the united states (which is indexed 1 in forbes2000$country) .Here's how the code runs. . I want the highlighted part. I tried doing this :

boxplot((log(Forbes2000$marketvalue[1])~Forbes2000$country[1]))

but it's incorrect.

Upvotes: 1

Views: 34

Answers (1)

neilfws
neilfws

Reputation: 33782

First, you don't need the double parentheses.

Second, try running Forbes2000$marketvalue[1]. You'll see that it returns the first value of the column marketvalue. So that's not what you want either.

What you need to do is select only those rows with the country of interest. One way to do that is subset. It may also be clearer to use the formula method for boxplot, so you can specify the data.

boxplot(log(marketvalue) ~ country, data = subset(Forbes2000, country == "United States"))

Result:

enter image description here

Upvotes: 2

Related Questions