0

I have a bunch of values I want to read in to an if statement in R. Namely:

year_1 , year_2
And so on. I would like to use a for loop or a vectorisation method to test each one but I am not familiar with this in R as opposed to C++. So I'd like to achieve something like:
for(i in 1:15) {
     if(year_[i] !=NULL) {
     count = count + 1
     }
}
Not sure whether I am not searching for the right thing or whether R just doesn't do this sort of thing easily. I have used paste and a for loop successfully in the past to automatically name new variables but this I haven't got a hold of.

Update Ok your answer seems to be on the right track. I should have been a bit more specific and say that I am reading the data from an excel file of parameters and using the data to produce plots. Different data sets will have different years active. The core of this problem is telling R how many years are active, starting from param$year_1 to param$year_15. So I am trying to actually read param$year_1 and so on for example, and basically check whether it is empty or not which will allow me to know how many years this particular data set is working with. When I tried

 mget(paste0("param$year_", 1:5)) 
it said the value was not found.

Update 2

I am sure the difficulty with this comes down to my description. But here is exactly what I want to produce but automated to a few lines as I know I will want to do similar operations like this is in the future. What the actual data is is irrelevant. This non automated version produces exactly what I want.

Non Automated Code



    if(is.na(param$year_1[1]) == TRUE || param$year_1[1] == '') {
    print("empty")
    }

    if(is.na(param$year_2[1]) == TRUE || param$year_2[1] == '') {
    print("empty")
    }

    if(is.na(param$year_3[1]) == TRUE || param$year_3[1] == '') {
    print("empty")
    }

    if(is.na(param$year_4[1]) == TRUE || param$year_4[1] == '') {
    print("empty")
    }

so on and so on until the final

    if(is.na(param$year_15[1]) == TRUE || param$year_15[1] == '') {
    print("empty")
    }

It is such a simple thing to do in C++ but I have to learn it in R for the future.

mike
  • 881
  • 2
  • 10
  • 23
  • The problems would seem more fundamental. In stead of many variables `year_1, year_2` etc you should probably use a single variable (a vector or list) – Robert Hijmans Oct 22 '15 at 04:01

1 Answers1

3

It sounds like you have a list-like structure that contains names year_1, year_2, ..., year_15 and you want to check how many of these are null or have a missing first element. You could use standard indexing to limit to the elements for those years, sapply to check which are null, and sum to add up those values:

which(sapply(paste0("year_", 1:15), function(x) {
  is.null(param[[x]]) || param[[x]][1] == ''
}))
#  year_2  year_5  year_9 year_10 year_11 year_12 year_14 year_15 
#       2       5       9      10      11      12      14      15 

Data:

param <- list(ID = 1:10, year_1 = 1:5, year_2 = NULL, year_3 = 1:7, year_4 = 1:2, year_5 = NULL, year_6 = 14, year_7 = 1:3, year_8 = 1:9, year_9 = NULL, year_10 = NULL, year_11 = NULL, year_12 = NULL, year_13 = 1:7, year_14 = NULL, year_15 = NULL)
josliber
  • 43,891
  • 12
  • 98
  • 133
  • Not quite. For others to use the script easily, it reads data from an excel file. This excel file has a number of different groups of data and the user specifies in the R script which one they would like and then it retrieves the respective data from the excel file. This years 1 to years 15 are already in the excel file (regardless of whether they have data in them for that data set), so some years will have no values. I need to tell the R script how many years are active for that group of data. – mike Oct 22 '15 at 04:29
  • @mike Could you work to include a reproducible example of your data and the desired result in the question itself? You should be able to use `dput`, as detailed in http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – josliber Oct 22 '15 at 04:31