如何识别R中两个不同列中观测值相同的行?

xxb16uws  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(114)

我有两列,我们称之为start_id和end_id,每一列都有字符串观察值,它们的结构必须保持为chr。
这是在一个有数百万行的数据集中。
我想创建一个新列来计算start_id是否等于end_id。如果它们相等,我希望它计算TRUE,否则计算FALSE。
我希望新列是永久的,而不是临时存储的。

df_new <- df %>% 
       mutate('new_column' = if_else('start_id' == 'end_id', TRUE, 'start_id' != 'end_id', FALSE))

#This ran without error, but when I looked at df_new, the evaluations were not correct. In fact, all returned as TRUE, when some should have been FALSE.

#Help a newb! Thanks!
6tqwzwtp

6tqwzwtp1#

在下面的内容中,我首先定义了一个包含两个char列的数据框,如您所说,"start_id"和"end_id",然后,我使用mutate和一个条件来创建新列。

# Create a data including two columns: start_id, end_id

start_id = c("one", "two", "three", "four", "five", "six", "seven")
end_id = c("one", "three", "two", "four", "seven", "six", "five")

df = data.frame(start_id, end_id)
df
# create a new column which is True if start_id equals end_id, otherwise it is False.
library(dplyr)
df = df %>% 
  mutate(status= if_else(.$start_id == .$end_id, 'True', 'False'))
df

相关问题