测试行中的列是否在数据帧中相等

时间:2016-04-25 11:08:48

标签: r

我有一个文件如下:

Item Loc1 Loc2 Loc3
1    JP   JP   JP
2    BP   BP   XP
3    LP   LP   LP

我想查看条件,看看Loc1 == Loc2 == Loc3(实际上我的列数多了很多)

因此我期待的输出是:

Item Loc1 Loc2 Loc3 Out
1    JP   JP   JP   TRUE
2    BP   BP   XP   FALSE
3    LP   LP   LP   TRUE

3 个答案:

答案 0 :(得分:3)

考虑所有列:

read.table(text="Item Loc1 Loc2 Loc3
1    JP   JP   JP
2    BP   BP   XP
3    LP   LP   LP", header=TRUE, stringsAsFactors=FALSE) -> df

df$Out <- apply(df, 1, function(x) { length(unique(x[-1]))==1 })

print(df)

##   Item Loc1 Loc2 Loc3   Out
## 1    1   JP   JP   JP  TRUE
## 2    2   BP   BP   XP FALSE
## 3    3   LP   LP   LP  TRUE

答案 1 :(得分:2)

一个选项是

 !rowSums(!df1[-1]==df1[2][row(df1[-1])])
 #[1]  TRUE FALSE  TRUE

答案 2 :(得分:1)

在@ hrbrmstr的响应中使用可重现的例子,一种方法如下:

product <- c("A", "B", "C", "D")
Loc1 <- c("NY","DN","CO","MI")
Loc2 <- c("NY","DN","CO","MI")
Loc3 <- c("NY","DN","PH","MI")
Loc4 <- c("NY","LA","CO","MI")
Loc5 <- c("NY","DN","CO","MI")

df <- data.frame(product, Loc1, Loc2, 
                 Loc3, Loc4, Loc5, 
                 stringsAsFactors=F)

df$out <- apply(df[, -1], 1, function(x) length(unique(x)) == 1)
df
#   product Loc1 Loc2 Loc3 Loc4 Loc5   out
# 1       A   NY   NY   NY   NY   NY  TRUE
# 2       B   DN   DN   DN   LA   DN FALSE
# 3       C   CO   CO   PH   CO   CO FALSE
# 4       D   MI   MI   MI   MI   MI  TRUE

此方法逐行循环遍历df,并检查位置列中唯一值的数量。如果它们全部相同,则长度将为1,因此如果它们全部相同则检查返回TRUE,否则返回FALSE