0

I have a column "Year" in my dataframe ("import") and I need to only select 2015 out of some 30 years. However none of the steps I tried worked. Things I tried include:

iy2015<-subset(import, import$year==2015)
iy2015<-import[which(import$year==2015),]
iy2015<-import[import$year==2015,]

all have given me an empty dataframe.

3
  • 5
    According to your comment on the answer below, you are using $year when it should be $Year. This is a typo and should be closed as such. Commented Nov 11, 2017 at 21:58
  • Thank you. I changed it to Year and it worked. Commented Nov 11, 2017 at 22:00
  • Don't use the df name and $ when using subset and noting that you say its name is "Year", then DO pay close attention to spelling. Try: iy2015<-subset(import, Year==2015) Commented Nov 12, 2017 at 14:59

1 Answer 1

1

For me your last option works, check if 2015 is in the column and check whether year is a column name. I used: iy2015 = import[import$Year==2015,]

EDIT:

You need to use Year instead of year.

Sign up to request clarification or add additional context in comments.

3 Comments

again I am getting an empty dataframe... here are results after I ran str(import)
'data.frame': 3809 obs. of 4 variables: $ Flow : logi TRUE TRUE TRUE TRUE TRUE TRUE ... $ Country: chr "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ... $ Trade : num 6.53e+09 7.72e+09 7.70e+09 8.55e+09 6.20e+09 ... $ Year : num 2016 2015 2014 2013 2012 ...
Does import$Year==2015 give a sequence with only one TRUE value?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.