我想知道有没有人能帮我把一个较长的行分成几个较短的行,然后再把它们拆开?
> df <- matrix(c("A", "B", "C"),nrow=3,ncol=12,byrow=F)
> df
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A"
[2,] "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B"
[3,] "C" "C" "C" "C" "C" "C" "C" "C" "C" "C" "C" "C"
> stack_df <- matrix(c(rep("A",3), rep("B",3), rep("C",3)), nrow = 9, ncol = 4)
> stack_df
[,1] [,2] [,3] [,4]
[1,] "A" "A" "A" "A"
[2,] "A" "A" "A" "A"
[3,] "A" "A" "A" "A"
[4,] "B" "B" "B" "B"
[5,] "B" "B" "B" "B"
[6,] "B" "B" "B" "B"
[7,] "C" "C" "C" "C"
[8,] "C" "C" "C" "C"
[9,] "C" "C" "C" "C"
> merge_df <- stack_df %>%
+ as.data.frame(.) %>%
+ unite(stack_df, 1:4, na.rm = T) %>%
+ print()
stack_df
1 A_A_A_A
2 A_A_A_A
3 A_A_A_A
4 B_B_B_B
5 B_B_B_B
6 B_B_B_B
7 C_C_C_C
8 C_C_C_C
9 C_C_C_C
> mutate_df <- cbind(sample(letters,9)) %>%
+ print()
[,1]
[1,] "w"
[2,] "q"
[3,] "t"
[4,] "p"
[5,] "r"
[6,] "k"
[7,] "i"
[8,] "o"
[9,] "d"
第3部分:解叠行(3行,3列)。(所需输出)
> simple_df <- matrix(mutate_df, nrow = 3, ncol=3, byrow = T)
> simple_df
[,1] [,2] [,3]
[1,] "w" "q" "t"
[2,] "p" "r" "k"
[3,] "i" "o" "d"
该问题将输入命名为df
,就好像它是一个数据帧,但它是一个矩阵,dplyr通常与数据帧而不是矩阵一起使用。dplyr可能不是这里使用的正确工具,而是使用这个base R one-liner,我们在末尾的注释中使用输入m,使用一个更精确的名称,并且还更改内容以使结果明确无误。
matrix(t(m), ncol = 4, byrow = TRUE)
给予:
[,1] [,2] [,3] [,4]
[1,] "A1" "A2" "A3" "A4"
[2,] "A5" "A6" "A7" "A8"
[3,] "A9" "A10" "A11" "A12"
[4,] "B1" "B2" "B3" "B4"
[5,] "B5" "B6" "B7" "B8"
[6,] "B9" "B10" "B11" "B12"
[7,] "C1" "C2" "C3" "C4"
[8,] "C5" "C6" "C7" "C8"
[9,] "C9" "C10" "C11" "C12"
或者,如果你打算让A一栏一栏地填写前3行,同样地让B和C一栏一栏地填写前3行
matrix(aperm(array(t(m), c(3, 4, 3)), c(1, 3, 2)), ncol = 4)
[,1] [,2] [,3] [,4]
[1,] "A1" "A4" "A7" "A10"
[2,] "A2" "A5" "A8" "A11"
[3,] "A3" "A6" "A9" "A12"
[4,] "B1" "B4" "B7" "B10"
[5,] "B2" "B5" "B8" "B11"
[6,] "B3" "B6" "B9" "B12"
[7,] "C1" "C4" "C7" "C10"
[8,] "C2" "C5" "C8" "C11"
[9,] "C3" "C6" "C9" "C12"
library(magrittr)
m %>% t %>% matrix(m, ncol = 4, byrow = TRUE)
m %>% t %>% array(c(3, 4, 3)) %>% aperm(c(1, 3, 2)) %>% matrix(ncol = 4)
m0 <- matrix(c("A", "B", "C"), 3, 12)
m <- replace(m0, TRUE, paste0(m0, col(m)))
m
给予:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] "A1" "A2" "A3" "A4" "A5" "A6" "A7" "A8" "A9" "A10" "A11" "A12"
[2,] "B1" "B2" "B3" "B4" "B5" "B6" "B7" "B8" "B9" "B10" "B11" "B12"
[3,] "C1" "C2" "C3" "C4" "C5" "C6" "C7" "C8" "C9" "C10" "C11" "C12"
最近我尝试使用ApacheFlink进行快速批处理。我有一个表,它有一个列:value和一个不相关的索引列 基本上我想计算每5行值的平均值和范围。然后我将根据我刚才计算的平均值计算平均值和标准偏差。所以我想最好的方法是使用窗口。 看起来是这样的 但是我不知道用。我试过,但它说没有这样的输入。我只希望它在从源代码读取时按顺序分组。但是它必须是一个时间属性,所以我不能使用索引列作为排序。 我是否必须添
输入=堆栈数 但是你只能弹出输入,你不能推到它。输出也是另一个堆栈,你可以返回并推到它,但不能弹出 所以如果 由于您无法在中返回到
我在R中创建了一个列表,大致如下: 〈代码〉主义者 我这样做是使用,我学到了应用排序--这意味着键是按字母数字排序的。 不幸的是,稍后,我需要按lappy中的位置访问这些值。我需要按照它们插入列表的顺序访问它们。 是否有任何方法可以从
有3个堆栈-A、B、C 堆栈A和B被排序(堆栈顶部的数字最大)。堆栈C为空,仅允许5次操作: 推,弹出,顶,is_empty,创建 我们需要编写一个函数来接收堆栈A和B,将堆栈A和B中的所有数字移动到堆栈C,堆栈C必须排序(最大数字在顶部)。 我有算法: 比较A的顶部和B的顶部 我开始写代码,但有错误,我不知道为什么! 代码:
我试图理解使用中给出的递归对堆栈元素进行排序http://www.geeksforgeeks.org/sort-a-stack-using-recursion/不允许使用while、for…等任何循环结构。我们只能在堆栈S上使用以下ADT函数: is_empty(S):测试堆栈是否为空。 push(S) :向堆栈添加新元素。 Pop(S):从堆栈中删除顶部元素。 top(S) :返回 top 元素
我已经在Java和C中找到了这个问题的几个实现,但我还没有找到一个使用JavaScript的示例。这是一个相当常见的技术面试问题: 在2n空间中对堆栈进行排序。(仅使用2个堆栈对堆栈进行排序)