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) ,] 
 })()
Jaap
  • 81,064
  • 34
  • 182
  • 193
Adam
  • 49
  • 6

1 Answers1

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.

Josh
  • 1,248
  • 12
  • 25
  • FWIW, for non-interactive use, `subset` is not recommended. – Roman Luštrik Aug 04 '15 at 10:38
  • @RomanLuštrik I've not heard that before, why is that? – Josh Aug 04 '15 at 10:39
  • 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." – Roman Luštrik Aug 04 '15 at 11:58
  • Thank you guys Josh u r right its work now thnaks – Adam Aug 04 '15 at 12:27