我有以下資料集:
ID<-c("A","B","C","D","E")
Fruits1<-c("orange","apple","pineapple","apple","pineapple")
Fruits2<-c("apple","orange","apple","pineapple","orange")
data<-tibble(ID,Fruits1,Fruits2)
data
# A tibble: 5 × 3
ID Fruits1 Fruits2
<chr> <chr> <chr>
1 A orange apple
2 B apple orange
3 C pineapple apple
4 D apple pineapple
5 E Pineapple orange
我想創建一個名為 FruitsDiff 的新列,它結合了 Fruits1 和 Fruits2 列,如下所示:
# A tibble: 5 × 4
ID Fruits1 Fruits2 FruitsDiff
<chr> <chr> <chr> <chr>
1 A orange apple apple-orange
2 B apple orange apple-orange
3 C pineapple apple apple-pineapple
4 D apple pineapple apple-pineapple
5 E pineapple orange orange-pineapple
我對這個新列的要求是水果按字母順序排序,無論哪個水果在資料框中排在第一位(例如,對于第 1 行,即使橙在蘋果之前,FruitsDiff 中的變數也是蘋果橙)。我通常使用 paste() 來組合列,但在這種情況下這對我沒有幫助。
有什么建議么?另外,兩個變數之間的分隔符可以是任何東西,為了示例,我只是使用了破折號。
uj5u.com熱心網友回復:
我們得到min/max'Fruit'列之間的元素pmin/pmax(其中最小/最大值將基于字母順序)和paste(str_c)來自這些函式的輸出以創建新列'FruitsDiff'
library(dplyr)
library(stringr)
data %>%
mutate(FruitsDiff = str_c(pmin(Fruits1, Fruits2),
pmax(Fruits1, Fruits2), sep = '-'))
-輸出
# A tibble: 5 × 4
ID Fruits1 Fruits2 FruitsDiff
<chr> <chr> <chr> <chr>
1 A orange apple apple-orange
2 B apple orange apple-orange
3 C pineapple apple apple-pineapple
4 D apple pineapple apple-pineapple
5 E pineapple orange orange-pineapple
或在 中base R,使用相同pmin/pmax的paste
data$FruitsDiff <- with(data, paste(pmin(Fruits1, Fruits2),
pmax(Fruits1, Fruits2), sep = '-'))
或使用applyand MARGIN = 1,回圈遍歷行、sort元素和pastewithcollapse作為引數
data$FruitsDiff <- apply(data[-1], 1, \(x) paste(sort(x), collapse = '-'))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/524545.html
標籤:r数据框按字母顺序
