我有下面的df
和用例,我想在所有行中找到并设置一些东西,这些行存在另一个满足条件的行。
df <- data.frame(X=c('a','b','c'), Y=c('a','c','d'))
> df
X Y
1 a a
2 b c
3 c d
我想在(另一个行)中找到Y值与X值相同的行。在上面的例子中,第2行是真的,因为Y = c
和第3行都有X = c
。请注意,第1行不满足条件。
类似于:
df$Flag <- find(df, Y == X_in_another_row(df))
发布于 2017-08-16 21:44:33
# Assume you want to use position 4, value 'c', to find all the rows that Y is 'c'
df <- data.frame(X = c('a', 'b', 'd', 'c'),
Y = c('a', 'c', 'c', 'd'))
row <- 4 # assume the desire row is position 4
val <- as.character( df[(row),'X'] ) # get the character and turn it into character type
df[df$Y == val,]
# Result
# X Y
# 2 b c
# 3 d c
https://stackoverflow.com/questions/45723103
复制相似问题