![](/img/trans.png)
[英]After subsetting a dataset based on a condition in one column, how to combine/add data rows from original data sharing same value in another column?
[英]How to transfer a column from a dataset sharing the same one with another one
我有兩個版本的數據集共享相同的列(或多或少)。 我們舉個例子
db = airquality
db1 = airquality[,-c(6)]
db1$Ozone[db1$Ozone < 30] <- 24
db1$Month[db1$Month == 5] <- 24
db
db1
如果我想通過使用 pipe 運算符 %>% 或其他迭代方法編寫代碼來將數據集 'db1' 中的兩列 'Ozone' 和 'Wind' 傳輸到 'db' 數據集以實現此結果,那么您可以編寫代碼可能會建議?
謝謝
你csn做:
library(dplyr)
db1 %>%
select(Ozone, Wind) %>%
bind_cols(db)
請注意,在此示例中,由於某些列名將在最終結果中重復,因此dplyr
將通過在列名末尾附加數字來自動重命名重復項。
底座 R:
cbind(db, db1[,c(1,3)])
Ozone Solar.R Wind Temp Month Day Ozone Wind
1 41 190 7.4 67 5 1 41 7.4
2 36 118 8.0 72 5 2 36 8.0
3 12 149 12.6 74 5 3 24 12.6
4 18 313 11.5 62 5 4 24 11.5
5 NA NA 14.3 56 5 5 NA 14.3
6 28 NA 14.9 66 5 6 24 14.9
7 23 299 8.6 65 5 7 24 8.6
8 19 99 13.8 59 5 8 24 13.8
9 8 19 20.1 61 5 9 24 20.1
10 NA 194 8.6 69 5 10 NA 8.6
11 7 NA 6.9 74 5 11 24 6.9
12 16 256 9.7 69 5 12 24 9.7
.
.
.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.