让我采用如下数据框:
>df : Col_1 Col_2 Col_3
A B z
C D x
A D c
B A g
D C f
现在,我需要订购这样的df:
>df1 : Col_1 Col_2 Col_3
A B z
C D x
A D c
A B g
C D f
所以,我需要在第一次尝试时按照它们出现的顺序排列这个数据框(这里df我们有A,B在行-1和B,A在第4行)。 我想把它们安排为A,B在第1行和A,B在第4行,如df1所示
答案 0 :(得分:3)
重新阅读问题后(在@ DavidArenburg的评论之后),一个可能的解决方案:
# order the letters rowwise in alphabetical order and paste them together in a character vector
x <- do.call(paste0, data.frame(t(apply(mydf[1:2], 1, function(x) x[order(x)]))))
# create an index for the first occurance
idx <- as.integer(factor(x, levels = unique(x)))
# replace with the first occurance
mydf[,-3] <- mydf[idx,-3]
给出:
> mydf
Col_1 Col_2 Col_3
1 B A z
2 C D x
3 A D c
4 B A g
5 C D f
使用过的数据:
mydf <- structure(list(Col_1 = structure(c(2L,3L,1L,2L,4L), .Label = c("A","B","C","D"), class = "factor"),
Col_2 = structure(c(1L,4L,4L,1L,3L), .Label = c("A","B","C","D"), class = "factor"),
Col_3 = structure(c(5L,4L,1L,3L,2L), .Label = c("c","f","g","x","z"), class = "factor")),
.Names = c("Col_1","Col_2","Col_3"), row.names = c(NA, -5L), class = "data.frame")
旧答案:您可以创建一个行的索引,其中Col_1
中的字母的序数高于Col_2
中的字母,然后只需交换这两个值:
# create a match vector
ltrs <- setNames(LETTERS,1:26)
# create an index for which rows the letter in 'Col_1' is of a higher order than in 'Col_2'
idx <- match(mydf$Col_1, ltrs) > match(mydf$Col_2, ltrs)
# swap the two values
mydf[idx,-3] <- mydf[idx, c(2,1)]
给出:
> mydf
Col_1 Col_2 Col_3
1 A B z
2 C D x
3 A D c
4 A B g
5 C D f