[英]matching between two data frame based on columns
我有一行的兩個數據框,每個數據框具有相同的列名。 數據幀之一在一個或多個列中具有NA值。 我想刪除其中一個數據幀中具有NA值的列,並刪除第二個數據幀中的相同列。
樣品:
數據框1:
age height education average
NA 1.80 college NA
數據框2:
age height education average
36 1.95 college 85
結果:
數據框1:
height education
1.80 college
數據框2:
height education
1.95 college
我該怎么做呢?
聽起來這些是數據幀,而不是向量。 如果將它們放到同一數據幀中(也許使用bind_rows()
),則可以使用dplyr一次處理它們,並找到沒有NA
值的所需列:
library(dplyr)
df <- tribble(
~age, ~height, ~education, ~average,
NA, 1.80, "college", NA,
36, 1.95, "college", 85
)
df %>%
select(which(!colSums(is.na(df))))
#> # A tibble: 2 x 2
#> height education
#> <dbl> <chr>
#> 1 1.80 college
#> 2 1.95 college
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.