I have a dataframe that is a column with all the state abbreviations:
我有一个数据框,它是一个包含所有州缩写的列:
Name
AK
AL
AR
AZ
CO
CT
DC
FL
I want to take this column and split it into multiple columns such that no column has more than 5 cells.
我想取这个列并将其拆分成多列,这样任何列都不会超过5个单元格。
Name1 Name2
AK CT
AL DC
AR FL
AZ
CO
I can create the code for what I want to do, but there has to 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
}
}
2 个解决方案
#1
5
If NA
is okay to use for the blank values, then we can do the following. Assuming your data is named df
, we can first create a vector of values to be used for splitting the data.
如果NA可以用于空白值,那么我们可以执行以下操作。假设您的数据名为df,我们可以先创建一个值向量来分割数据。
(x <- rep(1:ceiling(nrow(df) / 5), each = 5, length.out = nrow(df)))
# [1] 1 1 1 1 1 2 2 2
Now we can split the data, loop the resulting list to make each element length 5, and coerce to data frame. Column names are created on-the-fly here. It may be more efficient to create them afterward.
现在我们可以分割数据,循环结果列表以使每个元素长度为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>
#2
7
Similar to @RichScriven's concept, but using a matrix
to deal with the reshaping:
与@ RichScriven的概念类似,但使用矩阵来处理重塑:
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
#1
5
If NA
is okay to use for the blank values, then we can do the following. Assuming your data is named df
, we can first create a vector of values to be used for splitting the data.
如果NA可以用于空白值,那么我们可以执行以下操作。假设您的数据名为df,我们可以先创建一个值向量来分割数据。
(x <- rep(1:ceiling(nrow(df) / 5), each = 5, length.out = nrow(df)))
# [1] 1 1 1 1 1 2 2 2
Now we can split the data, loop the resulting list to make each element length 5, and coerce to data frame. Column names are created on-the-fly here. It may be more efficient to create them afterward.
现在我们可以分割数据,循环结果列表以使每个元素长度为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>
#2
7
Similar to @RichScriven's concept, but using a matrix
to deal with the reshaping:
与@ RichScriven的概念类似,但使用矩阵来处理重塑:
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