0

Im working on a project regarding information about movies including their release date.

My professor wants us to add a new column reflecting the decade the movies were released in (1980s, 1990s, 2000s, or 2010s). In the original spreadsheet, we have a "Release" column in the format %Y-%m-%d. I have already changed it from a factor to date using the command:

df$Release<-as.Date(df$Release, format = "%Y-%m-%d")

Now, again, he wants a new column with just the decade as stated earlier. This is where I hit a wall. We have not learned much in this class, so I attempted to do so using an if statement like so:

if(df$Release == "198"){
  df$Decade<- "1980s"
}

While I'm aware this is wrong this is the idea I had. I have not learned enough to really do much else! Im hoping to find an efficient command where i can take care of all 4 different decades using the given dates.

Here is a snapshot I took of my df so it may make more sense for you all

https://i.stack.imgur.com/VEo6G.png

Thanks for any help!

Casey
  • 1
  • You should probably read some introductory material rather than just posting here. [The R tag wiki has a lot of great resources](http://stackoverflow.com/tags/r/info). For binning data like this, you should use `cut`. [This is also a pretty common question on SO](http://stackoverflow.com/q/5570293/903061). – Gregor Thomas Feb 27 '17 at 22:02
  • Conceptually, think of your year as a numeric value, and you can round down to the nearest decade. I will let you search google or stackoverflow how to round a number down to specified number of digits in R and provide another attempt to solve the problem. – Djork Feb 27 '17 at 22:07
  • @Gregor Thanks for the advice. I've never been made aware of that command, Ill check it out! And I know the basics, I was just having a hard time deciding what command to use. I thought after finding this site that it was open to all questions, not just extremely difficult ones. – Casey Feb 27 '17 at 22:31
  • @R.S. This was a great idea, I had not even considered looking into that! Thanks a lot! Im giving it another go now! – Casey Feb 27 '17 at 22:32
  • The site is open to questions that aren't super-advanced, but most of the beginner questions have already been asked and answered many many times and don't need to be asked again. And there are enough mistakes/misconceptions evident in your code attempt that it looks like some introductory reading wouldn't hurt. – Gregor Thomas Feb 27 '17 at 22:59
  • @Gregor I understand what you're saying. We were just given some work to do that we received no instruction about, so this was me 'utilizing' outside resources like we were asked. And while there are many similar, I was just having a hard time extracting the year alone, then trying to bin it, and I couldnt locate an very similar example. That doesn't necessarily mean Im ignorant about the entire program! Not trying to sound malicious or anything of the sort, Im just saying! – Casey Feb 27 '17 at 23:11
  • No, of course you're not malicious. And I don't want to sound rude or off-putting---I hope you continue to use the site and become a member of the community! I'm trying to help you use the site effectively in the way intended. Don't take any of this personally - we just close your question as a duplicate, pointing to a resource for an answer. – Gregor Thomas Feb 27 '17 at 23:23
  • The advice I'd give is to search for each step in your process. You say "I was having a hard time extracting the year alone" - so search for ["[r] extract year"](http://stackoverflow.com/search?q=%5Br%5D+extract+year), I bet you one of the first three hits will clear that up. The duplicate was one of the first hits for "[r] bin data" or "[r] create bins" or something like that. – Gregor Thomas Feb 27 '17 at 23:23
  • The other advice I'd give is that **reproducible** questions tend to get good, fast answers. I don't know what your data looks like exactly - there's no code I can copy/paste *and run* to debug/make an example, so it's quicker and easier to close your question as a near-duplicate than to write an answer. [This Q/A covers lots of good practices for reproducible examples](http://stackoverflow.com/q/5963269/903061) - if your question shared a little data with `dput` it might have garnered an answer before being closed. – Gregor Thomas Feb 27 '17 at 23:26
  • @Gregor I was going to move to a chat so we didn't bother the other commenter but I didn't have enough reputation, but really thanks for all the help! Being brand new to this site is a little weird so thanks for all the advice and helpful links! You made this much more clear and now going into the future I wont be duplicating questions and looking silly! – Casey Feb 27 '17 at 23:55

0 Answers0