R - splitting a data frame with one column into several columns

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
  }
}
+4
source share
2 answers

If NAyou can use for empty values, we can do the following. Assuming your data is called df, we can first create a vector of values โ€‹โ€‹that will be used to separate the data.

(x <- rep(1:ceiling(nrow(df) / 5), each = 5, length.out = nrow(df)))
# [1] 1 1 1 1 1 2 2 2

, , 5, . . , .

as.data.frame(lapply(split(df$Name, paste0(names(df), x)), "length<-", 5))
#   Name1 Name2
# 1    AK    CT
# 2    AL    DC
# 3    AR    FL
# 4    AZ  <NA>
# 5    CO  <NA>
+5

@RichScriven, matrix :

columniser <- function(x, n) {
  m <- matrix(NA, nrow=n, ncol=ceiling(length(x)/n) )
  m[1:length(x)] <- x
  as.data.frame(m)
}

columniser(states$state.abb, 5)
#  V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
#1 AL CO HI KS MA MT NM OK SD  VA
#2 AK CT ID KY MI NE NY OR TN  WA
#3 AZ DE IL LA MN NV NC PA TX  WV
#4 AR FL IN ME MS NH ND RI UT  WI
#5 CA GA IA MD MO NJ OH SC VT  WY

columniser(1:12, 5)
#  V1 V2 V3
#1  1  6 11
#2  2  7 12
#3  3  8 NA
#4  4  9 NA
#5  5 10 NA
+7

Source: https://habr.com/ru/post/1660861/


All Articles