使用列的值内的条件按行排列df

问题描述 投票:0回答:1

假设我有一个这样的df

df1 <- data.frame(n =c("n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9", "n10", "n11", "n12", "n13", "n14", "n15", "n16", "n17", "n18"), Cond1 =c("I1", "I2", "I3", "I4", "I5", "I6", "I1", "I2", "I3", "I4", "I5", "I6", "I1", "I2", "I3", "I4", "I5", "I6"), Cond2 =c("c1", "c1","c1","c1","c1","c1","c2", "c2","c2","c2","c2","c2","c3","c3","c3","c3","c3","c3"))
df1

我按行进行采样

df2 <- df1[sample(nrow(df1)),]
df2

我想为采样设置条件,以便例如在列Cond2“c1”中具有列表的一个间隙,然后在下一行中再次出现。

因此,我希望行的随机顺序,但访问列的值,排序如果在新的df的前一行中cond2中有“c1”,则下一行不得包含“c1”,但是“ c2“或”c3“。

r dataframe permutation
1个回答
2
投票

你可以拿一个例子,例如是df1的两倍。然后利用Cond2列中的数字来设置差异并删除差异为0的所有行。最后将数据框缩小到df1的长度。

df2 <- df1[sample(nrow(df1), nrow(df1)*2, replace=TRUE), ]
df2$tmp <- diff(c(0, as.numeric(gsub("\\D", "", df2$Cond2))))
df2[df2$tmp != 0, -4][1:nrow(df1), ]
#        n Cond1 Cond2
# 2     n2    I2    c1
# 8     n8    I2    c2
# 4     n4    I4    c1
# 12   n12    I6    c2
# 3.1   n3    I3    c1
# 13   n13    I1    c3
# 11   n11    I5    c2
# 5     n5    I5    c1
# 11.1 n11    I5    c2
# 14   n14    I2    c3
# 1     n1    I1    c1
# 18   n18    I6    c3
# 3.2   n3    I3    c1
# 8.1   n8    I2    c2
# 13.2 n13    I1    c3
# 10.1 n10    I4    c2
# 15   n15    I3    c3
# 1.1   n1    I1    c1

要使多个列的解决方案适应,您可以使用while循环,因为它是一个长度未知的迭代过程,直到所有差异都是0

set.seed(42)  # for sake of reproducibility
df2 <- df1[sample(nrow(df1), nrow(df1)*2, replace=TRUE), ]

df2$tmp1 <- diff(c(0, as.numeric(gsub("\\D", "", df2$Cond1))))
df2$tmp2 <- diff(c(0, as.numeric(gsub("\\D", "", df2$Cond2))))

while (any(df2[4:5] == 0)) {
  df2 <- df2[df2$tmp1 != 0, ]
  df2 <- df2[df2$tmp2 != 0, ]
  df2$tmp1 <- diff(c(0, as.numeric(gsub("\\D", "", df2$Cond1))))
  df2$tmp2 <- diff(c(0, as.numeric(gsub("\\D", "", df2$Cond2))))
}

df2
#        n Cond1 Cond2 tmp1 tmp2
# 17   n17    I5    c3    5    3
# 6     n6    I6    c1    1   -2
# 15   n15    I3    c3   -3    2
# 12   n12    I6    c2    3   -1
# 14   n14    I2    c3   -4    1
# 3     n3    I3    c1    1   -2
# 12.1 n12    I6    c2    3    1
# 13   n13    I1    c3   -5    1
# 9     n9    I3    c2    2   -1
# 13.1 n13    I1    c3   -2    1
# 9.1   n9    I3    c2    2   -1
# 17.3 n17    I5    c3    2    1
# 3.1   n3    I3    c1   -2   -2
# 18.1 n18    I6    c3    3    2
# 2     n2    I2    c1   -4   -2
# 10.1 n10    I4    c2    2    1
# 17.5 n17    I5    c3    1    1
# 9.3   n9    I3    c2   -2   -1
# 16   n16    I4    c3    1    1
# 7     n7    I1    c2   -3   -1
# 15.2 n15    I3    c3    2    1
© www.soinside.com 2019 - 2024. All rights reserved.