检查 R 中一个数据帧中的两列是否具有相同的值 [除了 NA 之外]

发布于 2025-01-21 03:56:34 字数 832 浏览 2 评论 0原文

我有 1 个数据帧,j

Chr|Pos|A0|A1|rsID|Beta-A1|P|info|maf|se|rsid  
1|16021|C|T|NA|0.410|0.26|0.842|0.01|NA|rs1163602158   
1|17483|C|T|rs845637483|-0.356|0.32|0.856|0.01|NA|rs845637483    
1|19250|T|C|NA|-0.255|0.54|0.812|0.01|NA|rs7465843777   
1|39402|T|TCAA|NA|-0.873|0.37|0.821|0.01|NA|rs2746475333   
1|39883|G|C|NA|0.195|0.59|0.808|0.01|NA|rs2726463882

我想检查 rsID 和 rsid 中​​的行是否与前一列中的 NA 相同

所以我可以做

table(ifelse(j$rsID==j$rsid,"Yes","No"))
No      Yes
701232 18207968

而且我可以做

table(is.na(j$rsID))

FALSE     TRUE
18909200  2550533

table(is.na(j$rsid))

   FALSE
21459733

所以我可以看到那里有 701232 个不匹配的实例,但这些并不是全部,因为 NA,因为 NA 的数量比不匹配的实例多(2550533)?

有没有更好/更干净的方法来做到这一点,以便我可以更好地了解这一点?

谢谢

I have 1 dataframe, j:

Chr|Pos|A0|A1|rsID|Beta-A1|P|info|maf|se|rsid  
1|16021|C|T|NA|0.410|0.26|0.842|0.01|NA|rs1163602158   
1|17483|C|T|rs845637483|-0.356|0.32|0.856|0.01|NA|rs845637483    
1|19250|T|C|NA|-0.255|0.54|0.812|0.01|NA|rs7465843777   
1|39402|T|TCAA|NA|-0.873|0.37|0.821|0.01|NA|rs2746475333   
1|39883|G|C|NA|0.195|0.59|0.808|0.01|NA|rs2726463882

I want to check whether the rows in rsID and rsid are the same ASIDE from the NAs in the former column

So I can do

table(ifelse(j$rsID==j$rsid,"Yes","No"))
No      Yes
701232 18207968

And I can do

table(is.na(j$rsID))

FALSE     TRUE
18909200  2550533

table(is.na(j$rsid))

   FALSE
21459733

So I can see that there are 701232 instances where they don't match, but these are not ALL because of NA because there are MORE (2550533) NA than instances of them not matching?

Is there a better / cleaner way of doing this, so I can get a better idea of this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

可是我不能没有你 2025-01-28 03:56:34

可以删除Na,然后过滤它们不相等的地方:

library(dplyr)
library(tidyr)
j %>%
  drop_na(rsID, rsid) %>%
  filter(rsID != rsid) # Or == instead of != to keep where they are equal

Could remove NA then filter where they are not equal:

library(dplyr)
library(tidyr)
j %>%
  drop_na(rsID, rsid) %>%
  filter(rsID != rsid) # Or == instead of != to keep where they are equal
め七分饶幸 2025-01-28 03:56:34

另一个 dplyr 选项

j %>%
  rowwise() %>%
  mutate(duplicate = anyDuplicated(na.omit(c(rsid, rsID)))) %>%
  mutate(duplicate = ifelse(duplicate > 1, "Yes", "No")) %>% count(duplicate)

输出

# A tibble: 2 x 2
# Rowwise: 
  duplicate     n
  <chr>     <int>
1 No            4
2 Yes           1

Another dplyr option

j %>%
  rowwise() %>%
  mutate(duplicate = anyDuplicated(na.omit(c(rsid, rsID)))) %>%
  mutate(duplicate = ifelse(duplicate > 1, "Yes", "No")) %>% count(duplicate)

Output

# A tibble: 2 x 2
# Rowwise: 
  duplicate     n
  <chr>     <int>
1 No            4
2 Yes           1
甜宝宝 2025-01-28 03:56:34

我们可以使用基础R

with(na.omit(j[c('rsID', 'rsid')]),table(ifelse(rsID == rsid, "Yes", "No"))  )

We can use base R

with(na.omit(j[c('rsID', 'rsid')]),table(ifelse(rsID == rsid, "Yes", "No"))  )
情绪操控生活 2025-01-28 03:56:34
# Load dplyr library
library(dplyr, warn.conflicts = FALSE, quietly = TRUE)

# you already have j defined so this step is only for this demo
j <- tibble(Chr = c(1, 1, 1, 1, 1), 
            Pos = c(16021, 17483, 19250, 39402, 39883), 
            A0 = c("C", "C", "T", "T", "G"), 
            A1 = c("T", "T", "C", "TCAA", "C"), 
            rsID = c(NA, "rs845637483", NA, NA, NA), 
            `Beta-A1` = c(0.41, -0.356, -0.255, -0.873, 0.195), P = c(0.26,0.32, 0.54, 0.37, 0.59), 
            info = c(0.842, 0.856, 0.812, 0.821, 0.808), 
            maf = c(0.01, 0.01, 0.01, 0.01, 0.01), se = c(NA, NA, NA, NA, NA), 
            rsid = c("rs1163602158", "rs845637483", "rs7465843777","rs2746475333", "rs2726463882"))

# create a column is_same and use count()
j %>% 
  mutate(is_same = if_else(rsid == rsID, "Yes", "No", "No")) %>% 
  count(is_same)
#> # A tibble: 2 x 2
#>   is_same     n
#>   <chr>   <int>
#> 1 No          4
#> 2 Yes         1
# Load dplyr library
library(dplyr, warn.conflicts = FALSE, quietly = TRUE)

# you already have j defined so this step is only for this demo
j <- tibble(Chr = c(1, 1, 1, 1, 1), 
            Pos = c(16021, 17483, 19250, 39402, 39883), 
            A0 = c("C", "C", "T", "T", "G"), 
            A1 = c("T", "T", "C", "TCAA", "C"), 
            rsID = c(NA, "rs845637483", NA, NA, NA), 
            `Beta-A1` = c(0.41, -0.356, -0.255, -0.873, 0.195), P = c(0.26,0.32, 0.54, 0.37, 0.59), 
            info = c(0.842, 0.856, 0.812, 0.821, 0.808), 
            maf = c(0.01, 0.01, 0.01, 0.01, 0.01), se = c(NA, NA, NA, NA, NA), 
            rsid = c("rs1163602158", "rs845637483", "rs7465843777","rs2746475333", "rs2726463882"))

# create a column is_same and use count()
j %>% 
  mutate(is_same = if_else(rsid == rsID, "Yes", "No", "No")) %>% 
  count(is_same)
#> # A tibble: 2 x 2
#>   is_same     n
#>   <chr>   <int>
#> 1 No          4
#> 2 Yes         1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文