将单元格移动到每个第48行的新列

我有名单在A1:A144,我想移动A49:A96到B1:B48和A97:144到C1:C48。

所以对于第48行,我希望接下来的48行移动到一个新列。

怎么做?

如果你想考虑一个VBA替代scheme,那么:

Sub MoveData() nF = 1 nL = 48 nSize = Cells(Rows.Count, "A").End(xlUp).Row nBlock = nSize / nL For k = 1 To nBlock nF = nF + 48 nL = nL + 48 Range("A" & nF & ":A" & nL).Copy Cells(1, k + 1) Range("A" & nF & ":A" & nL).ClearContents Next k End Sub 

不知道该解决scheme的可扩展性如何,但它确实有效。

首先让我们假装你的名字是x ,你想要的解决scheme在new.df

 number.shifts <- ceiling(length(x) / 48) # work out how many columns we need # create an empty (NA) data frame with the dimensions we need new.df <- matrix(data = NA, nrow = length(x), ncol = number.shifts) # run a for-loop over the x, shift the column over every 48th row j <- 1 for (i in 1:length(x)){ if (i %% 48 == 0) {j <- j + 1} new.df[i,j] <- x[i] } 

我想你必须再详细说一下你的问题。 你有R或Excel中的数据,你想输出在R或在Excel中?

那个beeing说,如果x是你的向量表示集群

 x <- rep(1:3, each = 48) 

y是包含名称或任何想要分布在列A:C(每个具有48行)的variables,

 y <- sample(letters, 3 * 48, replace = TRUE) 

你可以这样做:

 y.wide <- do.call(cbind, split(y, x)) 

就像在R中有stack来创build一组列的非常长的表示一样,有一个stack需要一个长的列,并把它变成一个宽的forms。

这是一个基本的例子:

 mydf <- data.frame(A = 1:144) mydf$groups <- paste0("A", gl(n=3, k=48)) ## One of many ways to create groups mydf2 <- unstack(mydf) head(mydf2) # A1 A2 A3 # 1 1 49 97 # 2 2 50 98 # 3 3 51 99 # 4 4 52 100 # 5 5 53 101 # 6 6 54 102 tail(mydf2) # A1 A2 A3 # 43 43 91 139 # 44 44 92 140 # 45 45 93 141 # 46 46 94 142 # 47 47 95 143 # 48 48 96 144