Once again here trying to do things without the use of for
, I have a problem that has a solution with for
but I would like to give it a focus without for
.
The problem is that I have some data with days and I want to create a variable in the same data that is the month to which these data correspond.
inicio <- c("01012018","01022018","01032018","01042018","01052018","01062018",
"01072018","01082018","01092018","01102018","01112018","01122018")
fin <- c("31012018","28022018","31032018","30042018","31052018","30062018",
"31072018","31082018","30092018","31102018","30112018","31122018")
mes <- c("enero","febrero","marzo","abril","mayo","junio",
"julio","agosto","septiembre","octubre","noviembre","diciembre")
fechas <- data.table(inicio,fin,mes)
dias <- c("01032018","02042018","14062018","13012018","20102018")
datos <- data.table(dias)
datos$mes <- data.table(ifelse(fechas$inicio <= datos$dias & datos$dias <= fechas$fin,fechas$mes,"error"))
The output I get is the following:
dias mes
1: 01032018 enero
2: 02042018 febrero
3: 14062018 marzo
4: 13012018 abril
5: 20102018 mayo
How can you buy the solution is not correct, in addition, I get the following notice:
Warning messages:
1: In fechas$inicio <= datos$dias :
longer object length is not a multiple of shorter object length
2: In datos$dias <= fechas$fin :
longer object length is not a multiple of shorter object length
3: In '[<-.data.table'(x, j = name, value = value) :
Supplied 12 items to be assigned to 5 items of column 'mes' (7 unused)