我想比较数据框中的变量“Name”,如果值相同,则打印该值,这就是我正在做的事情:
{{1}}
答案 0 :(得分:1)
通常,处理此类问题的正确方法是通过merge
或加入语义:
# added stringsAsFactors=FALSE to avoid treating names as factors
x <- data.frame("SN" = 1:4,
"Age" = c(21,15,30,40),
"Name" = c("Isa","Dora","Luisa","Daniela"),
stringsAsFactors = FALSE)
y <- data.frame("SN" = 1:4,
"Age" = c(22,17,36,41),
"Name" = c("Isa","Cristian","Peter","Juan"),
stringsAsFactors = FALSE)
z <- merge(x, y, by="Name")[["Name"]]
z
[1] "Isa"
话虽如此,我不建议按名称加入两个数据集(或进行任何比较),原因有两个。首先,细微的拼写问题会导致您无法匹配您可能希望匹配的记录(尝试将y
中的名称转换为小写并重新运行代码)。其次,名称通常是非唯一的,您必须解决重复数据删除的艰巨任务。