Update:经过研究,我发现:What are the differences between "=" and "<-" assignment operators in R?
@Konrad Rudolf的答案讨论了=
和<-
之间的区别,有趣的是,用<-
代替=
会抛出一个错误:
# in contrast to sad[, a = anything_here]
sad[, a <- anything_here]
error: Error in `[.tbl_df`(sad, , a <- anything) : object 'anything' not found`
如果你用<-
,也可以用your example
sad[, a <- sad[, "Ma"] == "Y"]
从@Nosredna所提到的网站回答中得出结论:
"谷歌的R风格指南通过禁止在作业(job)中使用"="来简化问题.这是一个不错的 Select ."
So all in all maybe it is an assignement issue.
First answer:
让我们考虑一下:把右边的逗号分解成columns.另一方面,逗号的左侧将对行进行操作:
还可以考虑@onyanbu在 comments 中指出的:
# will not work no column a (a is a object)
sad[, a]
# All rows and the column named "a" -> this will work
sad[, 'a']
# will not work because you want to get all rows and column "Ma" -> there is no colum Ma in sad
sad[, "Ma"]
# here you assign nothing to object a
a = sad[, "Ma"]
# consecutevily you compare a = sad[, "Ma"] (e.g. nothing) to string 'Y'
# will not work because 'Ma' column don't exist.
sad[, "Ma"] == "Y"