1

Despite the fact that I see the object that I was looking for, I can't understand why it can't be found.

sapply(tuPeru1naomit, class)
    Month      YEAR      LONG       LAT      Temp 
 "factor" "integer" "numeric" "numeric" "numeric" 

head(tuPeru1naomit)
  Month YEAR  LONG   LAT    Temp
1   JAN 1980 -39.9 -89.5 17.5498
2   JAN 1980 -39.5 -89.5 17.8718
3   JAN 1980 -39.2 -89.5 18.1983
4   JAN 1980 -38.9 -89.5 18.5264
5   JAN 1980 -38.5 -89.5 18.8529
6   JAN 1980 -38.2 -89.5 19.1596

God1980<-subset(tuPeru1naomit,Month==JAN,YEAR==1980)

Error in eval(e, x, parent.frame()) : object 'JAN' not found

  • Possible duplicate https://stackoverflow.com/q/27886839/680068 – zx8754 Oct 26 '22 at 12:23
  • @zx8754, I agree that that link is useful, but it does not address the possibility that the OP is trying to reference a string literal instead of a non-existing object. Granted, _this_ OP has not verified my suspicion on that theory ... – r2evans Oct 26 '22 at 12:26

1 Answers1

1
  1. Month == JAN is looking for two objects: Month (found within tuPeru1naomit) and JAN (does not exist). I believe you intend to be looking for the value/string "JAN", so Month == "JAN".

  2. The third argument to subset(..) is select=, a non-standard-eval column selector. In your call, though, it appears that you are using it for a second subsetting condition. You need to combine your conditionals with & (logical AND) or | (logical OR), I'll assume the first.

subset(tuPeru1naomit, Month == "JAN" & YEAR == 1980)
#   Month YEAR  LONG   LAT    Temp
# 1   JAN 1980 -39.9 -89.5 17.5498
# 2   JAN 1980 -39.5 -89.5 17.8718
# 3   JAN 1980 -39.2 -89.5 18.1983
# 4   JAN 1980 -38.9 -89.5 18.5264
# 5   JAN 1980 -38.5 -89.5 18.8529
# 6   JAN 1980 -38.2 -89.5 19.1596

If you're curious about the use of the select= argument, all of the following are feasible options:

# range of columns
subset(tuPeru1naomit, Month == "JAN" & YEAR == 1980, select = LONG:Temp)
# individual columns, using non-standard-evaluation
subset(tuPeru1naomit, Month == "JAN" & YEAR == 1980, select = c(Month, LONG))
# same, using standard-evaluation (strings)
subset(tuPeru1naomit, Month == "JAN" & YEAR == 1980, select = c("Month", "LONG"))
# all except the specified columns
subset(tuPeru1naomit, Month == "JAN" & YEAR == 1980, select = -c(LONG))
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Now there isn't problem with 'Month', but with 'YEAR' : ```God1980<-subset(tuPeru1naomit,Month=="JAN",YEAR==1980,select= c(Month,YEAR,LAT,LONG,Temp))```#Error in [.data.frame(x, r, vars, drop = drop) : # object 'YEAR' not found – Джимми Мартина Oct 26 '22 at 15:54
  • 1
    *Read the whole answer.* Your `Month=="JAN",YEAR==1980` _is wrong_. Change the comma to an `&` and it should work. – r2evans Oct 26 '22 at 16:00
  • How must it be done if I'd only like to choose LONG and LAT for each half (e.g. 39.5, 38.5, 37.5 and so on)? – Джимми Мартина Oct 28 '22 at 11:56
  • I don't understand: choosing `LONG` and `LAT` has nothing to do with filtering based on `Month` and `YEAR`. Your _filtering_ arguments are incorrect, don't separate additional filtering requirements by commas, instead use logical combiners such as `&` and/or `|` (and parentheses if complex logic is required). For your question, what is "each half"? That isn't clear in the current context. – r2evans Oct 28 '22 at 12:24
  • I understand your suggestion. ```God1980<-subset(tuPeru1naomit,Month=="JAN"&YEAR==1980,select= c(Month,YEAR,LAT,LONG,Temp))``` perfect with this. Furthermore, is it possible to have column's information only for LONG -39.5, -38.5, -37.5 ?. Perhaps, the only way is ```God1980<-subset(tuPeru1naomit,Month=="JAN" & YEAR==1980 & LONG==39.5 & LONG==38.5 & LONG == 37.5 & LONG == 36.5 & LONG == 35.5 ... ,select= c(Month,YEAR,LAT,LONG,Temp))``` ? – Джимми Мартина Oct 28 '22 at 13:01
  • On what logic are those three observations chosen? Is it range-based? Especially consider the precision typically involved when using latitude/longitude data, looking for specific values of these floating-point numbers is highly likely to fail based on the difficulties of encoding floating-point irrational numbers in digital storage (not just an R thing), see https://stackoverflow.com/q/9508518/3358272, https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f. – r2evans Oct 28 '22 at 13:26