r/R_Programming • • Nov 25 '17

Subsetting Problem

Hi everyone,

New to this subreddit. I'm in a Big Data class in school and we're using R. So far, so good, but I'm running into an issue with subsetting.

Our project is to create graphs based on a large csv which shows website traffic data from our school. We are supposed to use only the United States, but the data shows many other countries.

I thought I subsetted the data correctly, and when I do summary() it shows how I want it to - by filtering out all the other countries.

Within this data are regions - aka states. I would like to use R to make a barplot that shows only "regions" of the United States. To do this, I used the subset I created, however, the plot shows ALL countries and regions, which gets super cluttered!

Here's an example of what I did:

America <- webtest[webtest$Country=="United States", ] 

barplot(table(webtest),
    col = rainbow(3),
    ylab = "Count",
    xlab = "State",
    ylim= c(0,50000),
    main = "Barplot of Frequency of States",
    las = 2)

Any help would be much appreciated. Thanks!

Edit: Sample data

Focus	   Country	   Region	    City	   Datehour	Entrances	Visitors
Admissions	Pakistan	(not set)	Islamabad	2012112500	1	1
Admissions	Pakistan	(not set)	Islamabad	2012112500	0	1
Admissions	Singapore	(not set)	Singapore	2012112500	1	1
Admissions	USA  	    California	Concord	    2012112500	0	1
Admissions	USA  	    California	Concord	    2012112500	0	1
Admissions	USA  	    California	Concord	    2012112500	0	1
0 Upvotes

9 comments sorted by

View all comments

2

u/gruyereparty Nov 25 '17

Just realized bar(table(webtest) should show America.

1

u/Darwinmate Nov 26 '17

Riiight I think I know where you're getting confused.

When you subset using:

America <- webtest[webtest$Country=="United States", ] 

You are creating a new object called America. It does not alter the object webtest in anyway.

1

u/gruyereparty Nov 26 '17

Oooooo thank you! How would I alter webtest?

1

u/Darwinmate Nov 26 '17

The same way you created Amerca you can alter webtest. But you're not actually altering, you're replacing webtests with a subsetted version of it. So what you're doing currently, keeping webtest and creating a subset called America, is the recommended way