0

let say i have dataset for State , year , week and location in csv format i want retrieve all data for specific state for year 2014 from week 36 to week 53 as follow

i've already do this but still have another not need states, thnx

LH14 <- (function(){
 x <- read.csv("LocationHotspot2014.csv")
 x[,"State"] <- toupper(lh14[,"State"])
 x[x$State=="California",],
 x[x$Week== c(36:53) ,] 
 })()
1
  • You will need %in% insteed of == for checking whther the weeks match. Commented Aug 4, 2015 at 9:54

1 Answer 1

2

Your code has several errors. This should run, assuming the csv file exists and has the appropriate columns.

LH14 <- function() { 
  x <- read.csv("LocationHotspot2014.csv")
  x$State <- toupper(x$State)
  x <- x[x$State == "CALIFORNIA" & x$Week %in% 36:53, ]
  x
}
lh14 <- LH14()

edit: Replaced subset with indexing per Roman's suggestion.

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

4 Comments

FWIW, for non-interactive use, subset is not recommended.
@RomanLuštrik I've not heard that before, why is that?
See ?subset: "This is a convenience function intended for use interactively. For programming it is better to use the standard subsetting functions like [, and in particular the non-standard evaluation of argument subset can have unanticipated consequences."
Thank you guys Josh u r right its work now thnaks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.