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 :
But it shows the warning :
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
}
}
Code:
the condition has length > 1 and only the first element will be used