【问题标题】:Returning a value of column A if column B has corresponding unique values in R如果列 B 在 R 中具有相应的唯一值,则返回列 A 的值
【发布时间】:2021-07-01 07:25:23
【问题描述】:

示例数据框

index     sample 
1           x                    
1           x
1           x
1           y
2           z
2           z
2           z
3           x
3           y
3           y
3           z

我想创建一个循环,如果相应的样本值不同,它将遍历索引列表并返回它。我正在尝试这样的事情,但我不太确定如何实现它。

for i in 1:length(index){
if(index == 1) {
check for sample values{
if there are more than one distinct value{ 
print(index)
else{
index + 1}}}}}

理想情况下,返回的向量是 [1] 1 3

因为 index == 2 包含所有相同的值

提前致谢!

【问题讨论】:

标签: r dataframe loops unique


【解决方案1】:

这是使用aggregatesubset 的基本R 解决方案:

subset(aggregate(sample ~ index, function(x) length(unique(x)), data = df), sample > 1)$index

[1] 1 3

通常,在 R 中使用循环是非常低效的,而且大多数时候有更好(更快)的替代方案。

这是我使用的数据

df <- structure(list(index = c(1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3), sample = c("x", 
"x", "x", "y", "z", "z", "z", "x", "y", "y", "z")), row.names = c(NA, 
-11L), class = "data.frame")

【讨论】:

    【解决方案2】:

    这行得通吗:

    library(dplyr)
    df %>% group_by(index) %>% filter(n_distinct(sample)>1) %>% pull(index) %>% unique
    [1] 1 3
    

    【讨论】:

      猜你喜欢
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      • 2022-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多