I have a dataframe, which is a column with all state abbreviations:
Name
AK
AL
AR
AZ
CO
CT
DC
FL
I want to take this column and split it into several columns so that there are no more than 5 cells in the column.
Name1 Name2
AK CT
AL DC
AR FL
AZ
CO
I can create code for what I want to do, but there should be a better way:
states <- as.data.frame(state.abb)
new.table <- as.data.frame(states[1:5,])
i <- 6
k <- 2
repeat{
new.table[,k] <- as.data.frame(states[(i):(i+4),])
i <- i + 5
k <- k + 1
if(i>nrow(states)){
break
}
}
source
share