I get a problem about "subscript out of bounds", what I want is to get the first and last month for each observation where I have three consecutive "1" or "True". I want to create 2 new column "begin" and "end" where I get repectively the first month and the last month. In my example for the first observation : begin equal to avril and end equal to juin In the 5 observation : begin equal to fevrier and end equal to avril In the 9 observation : begin equal to janvier and end equal to mars ...
I tried to do this :
nom <- letters[1:5]
pseudo <- paste(name, 21:25, sep = "")
janvier <- c(0, 1, 1, 1, 0)
fevrier <- c(1, 1, 1, 1, 1)
mars <- c(0, 0, 0, 1, 1)
avril <- c(1, 1, 1, 0, 1)
mai <- c(1, 0, 1, 1, 1)
juin <- c(1, 1, 0, 1, 0)
df <- data.frame(nom =nom, pseudo = pseudo, janvier = janvier,
fevrier = fevrier, mars = mars, avril = avril,
mai = mai, juin = juin)
dfm <- as.matrix(df[, -c(1, 2)])
my_matrix <- matrix(nrow = 10, ncol = 6)
for(i in 1:dim(dfm)[1]){
for(j in 1:dim(dfm)[2]){
if(dfm[i, j] + dfm[i, j+1] + dfm[i, j+2] == 3){
my_matrix[i, j] <- "periode_ok"
my_matrix[i, j+1] <- "periode_ok"
my_matrix[i, j+2] <- "periode_ok"
}
}
}
The ouput should be this :
begin <- c("avril", "no info", "no info",
"janvier", "fevrier", "avril", "no info",
"no info", "janvier", "fevrier")
end <- c("juin", "no info", "no info", "mars",
"avril", "juin", "no info", "no info",
"mars", "avril")
output <- data.frame(nom =nom, pseudo = pseudo, janvier = janvier,
fevrier = fevrier, mars = mars, avril = avril,
mai = mai, juin = juin, begin = begin,end = end)
Any help will be apreciated