R warning : the condition has length > 1 and only the first element will be used

#1
I have data like following :

year month day y
2007 7 1 3845
2007 7 2 3756
2007 7 3 3564
.
.
.
2007 7 31 3478
2007 8 1 3245
2007 8 2 3764
.
.
.
2007 8 31 3123
.
.
.
2008 12 31 3890


Now I want to create a new column in the data frame named "month_index" which will take value 7 when year==2007 and month==7; then

month_index == 8, when year==2007 and month==8;
month_index == 9, when year==2007 and month==9;
.
.
.
month_index == 18, when year==2008 and month==12.

So I wrote the R code as :

Code:
month_index <- NULL
k <- 1

for (i in 2007:2008){
  for(j in 1:12){
    if(data$year == i &  data$month == j) month_index <- k
    k <- k+1
  }
}
But it shows the warning :

Code:
the condition has length > 1 and only the first element will be used
 

trinker

ggplot2orBust
#2
It seems like your month index here should be 24. Is this a typo? If not please explain the logic of how to calculate month_index as I think the for loop is the least desirable way to go.

month_index == 18, when year==2008 and month==12
 

trinker

ggplot2orBust
#3
I suspect you want to make a date key and then merge with a left join on your data set:

Code:
if (!require("pacman")) install.packages("pacman")
pacman::p_load(dplyr, tidyr)

[COLOR="gray"]## fake data I had to make,  In the future of posting to help lists please do this yourself[/COLOR]
dat <- data_frame(
    date = as.character(seq(as.Date("2007/1/1"), as.Date("2008/12/31"), "days"))
) %>%
    separate(date, c('year', 'month', 'day'), sep="-") %>%
    mutate_each(funs(as.numeric)) %>%
    mutate(y = sample(2500:4500, nrow(.), TRUE))


key <- data_frame(
    date = as.character(seq(as.Date("2007/1/1"), as.Date("2008/12/31"), "months"))
) %>%
    separate(date, c('year', 'month', 'day'), sep="-") %>%
    mutate_each(funs(as.numeric)) %>%
    select(-day) %>%
    mutate(month_index = 1:n())

left_join(dat, key)
 
#5
If the year variable is treated as a factor the index variable can be created with simple arithmetic:

Code:
df$month_index <- with(df, (as.numeric(as.factor(year)) -1) * 12 + month)