拆分列表每n个元素和cbind,然后是rbind切片

时间:2015-07-27 15:13:34

标签: r list dplyr rbind cbind

我想切片列表的每个 n 元素,切片cbind,然后切片rbind

我可以使用下面的代码执行此操作(n = 10个元素,列表长度为30个元素)。我手动'选择列表中的每10个元素,然后cbind这10个元素切片。然后我rbind那些cbind个ed切片。

但是,我认为l*plyplyr中的dplyr或至少其中一些dl <- list(c(2L, 1L, 3L, 2L, 1L, 1L, 3L), c(1L, 1L, 2L, 1L, 1L, 2L, 1L), c(1L, 1L, 2L, 2L, 3L, 3L, 3L), c(1L, 1L, 2L, 2L, 3L, 3L, 3L), c(1L, 1L, 2L, 2L, 3L, 3L, 3L), c(1L, 1L, 2L, 2L, 3L, 3L, 1L), c(1L, 1L, 2L, 2L, 3L, 3L, 3L), c(1L, 3L, 2L, 1L, 3L, 2L, 1L), c(3L, 1L, 2L, 3L, 3L, 1L, 3L), c(3L, 2L, 1L, 1L, 3L, 3L, 1L), c(1L, 1L, 2L, 2L, 2L, NA, NA), c(1L, 1L, 2L, 2L, 3L, NA, NA), c(1L, 1L, 2L, 2L, 3L, NA, NA), c(1L, 1L, 2L, 2L, 3L, NA, NA), c(1L, 1L, 2L, 2L, 3L, NA, NA), c(1L, 1L, 2L, 2L, 3L, NA, NA), c(1L, 1L, 2L, 2L, 3L, NA, NA), c(1L, 1L, 2L, 2L, 3L, NA, NA), c(1L, 1L, 2L, 2L, 3L, NA, NA), c(2L, 1L, 2L, 1L, 1L, NA, NA), c(2L, 3L, 1L, 2L, 1L, 2L, NA), c(1L, 1L, 2L, 2L, 1L, 3L, NA), c(1L, 1L, 2L, 2L, 3L, 3L, NA), c(1L, 1L, 2L, 2L, 3L, 3L, NA), c(1L, 1L, 2L, 2L, 3L, 3L, NA), c(1L, 1L, 2L, 2L, 3L, 3L, NA), c(1L, 1L, 2L, 2L, 3L, 3L, NA), c(1L, 1L, 2L, 2L, 3L, 3L, NA), c(1L, 1L, 2L, 2L, 3L, 3L, NA), c(1L, 1L, 2L, 2L, 3L, 3L, NA)) # slice list 'manually' cbind those slices dl1 <- dl[1:10] dl1.c <- do.call("cbind", dl1) dl2 <- dl[11:20] dl2.c <- do.call("cbind", dl2) dl3 <- dl[21:30] dl3.c <- do.call("cbind", dl3) # rbind the cbind slices for result ans <- as.data.frame(rbind(dl1.c, dl2.c, dl3.c)) # ans as df # ans <- rbind(dl1.c, dl2.c, dl3.c) 可能会有这种方法。对于初学者,我现在不知道如何选择列表中的每个 n 元素,并且似乎不知道相应的搜索词来找到这个答案。

public void Main()
        {
            // TODO: Add your code here
            string directoryPath = Dts.Variables["User::DestinationFilePath"].Value.ToString();
            string[] oldFiles = System.IO.Directory.GetFiles(directoryPath, Dts.Variables["User::VarFileName"].Value.ToString());
            foreach (string currFile in oldFiles)
            {
                FileInfo currFileInfo = new FileInfo(currFile);
                currFileInfo.Delete();

            }
            Dts.TaskResult = (int)ScriptResults.Success;
        }

1 个答案:

答案 0 :(得分:6)

尝试

do.call(mapply, c(cbind, split(dl, cut(seq_along(dl), length(dl)/10, labels = FALSE))))