我正在使用名为sells
的数据框,并上传到此链接http://www.filedropper.com/weekpurchases18-25
它有一个名为TitleCli
的列,我更正其值如下:
> library(stringr)
> sells$TitleCli <- str_trim(sells$TitleCli)
我有另一个名为Gender
的专栏,我填写如下:
ifelse(sells$TitleCli == "MR", sells$Gender<-"F", "M")
该命令的结果是:
[1] "M" "M" "M" "F" "M" "M" "M" "M" "M" "M" "M" "M" "M" "M" "M" "M" "M" "M" "M" "M"
[21] "F" "M" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "M" "M" "M" "F" "M" "M" "F" "F"
[41] "F" "M" "M" "F" "F" "M" "F" "F" "F" "F" "M" "M" "M" "M" "M" "F" "M" "F" "M" "F"
[61] "M" "F" "M" "F" "M" "F" "M" "M" "F" "M" "M" "F" "F" "F" "F" "F" "M" "M" "M" "M"
[81] "F" "F" "M" "F" "M" "M" "M" "F" "F" "F" "F" "M" "M" "F" "M" "M" "F" "M" "F" "F"
[101] "F" "F" "F" "F" "F" "F" "F" "M" "M" "M" "M" "M" "F" "F" "M" "M" "F" "M" "F" "M"
[121] "M" "F" "F" "F" "F" "M" "M" "F" "F" "M" "M" "F" "M" "M" "F" "F" "M" "M" "F" "M"
[141] "M" "M" "M" "F" "F" "M" "M" "M" "F" "M" "M" "M" "F" "M" "M" "M" "F" "M" "F" "F"
[161] "M" "M" "M" "M" "M" "F" "M" "F" "M" "F" "M" "M" "F" "F" "M" "M" "M" "F" "F" "M"
[181] "F" "F" "M" "F" "F" "F" "M" "M" "M" "M" "F" "F" "M" "M" "M" "M" "M" "M" "M" "M"
[201] "M" "F" "F" "M" "F" "F" "M" "M" "M" "F" "F" "M" "F" "M" "F" "M" "M" "F" "M" "M"
[221] "M" "F" "M" "M" "M" "M" "M" "M" "M" "M" "F" "M" "M" "M" "M" "M" "M" "M" "M" "M"
[241] "F" "F" "M" "F" "F" "M" "M" "F" "F" "M" "M" "F" "F" "F" "F" "F" "M" "M" "M" "M"
[261] "M" "M" "M" "M" "F" "M" "F" "F" "M" "F" "M" "M" "M" "F" "M" "F" "F" "M" "F" "F"
[281] "F" "M" "F" "F" "F" "M" "M" "F" "F" "F" "F" "F" "M" "M" "F" "M" "M" "M" "M" "M"
[301] "M" "F" "F" "F" "F" "M" "F" "F" "F" "M" "M" "M" "F" "M" "F" "M" "M" "M" "M" "F"
[321] "F" "M" "F" "F" "F" "F" "M" "F" "F" "M" "M" "F" "F" "M" "F" "M" "M" "M" "F" "F"
[341] "M" "M" "M" "M" "M" "F" "M" "F" "M" "M" "M" "M" "M" "F" "F" "M" "M" "M" "F" "M"
[361] "F" "F" "M" "F" "M" "M" "M" "M" "M" "M" "M" "M" "M" "F" "M" "M" "F" "M" "M" "M"
[381] "F" "M" "M" "M" "M" "M" "F" "M" "M" "M" "M" "F" "M" "M" "M" "F" "M" "F" "F" "F"
[401] "F" "M" "M" "M" "M" "M" "F" "M" "M" "M" "M" "M" "M" "M" "M" "F" "M" "F" "F" "F"
[421] "F" "F" "F" "M" "F" "M" "F" "M" "M" "M" "M" "F" "F" "M" "F" "M" "M" "F" "M" "F"
[441] "M" "F" "M" "F" "F" "M" "F" "M" "F" "M" "F" "F" "M" "M" "M" "M" "M" "M" "F" "F"
[461] "F" "F" "F" "M" "F" "F" "F" "F" "M" "M" "F" "M" "F" "F" "F" "M" "M" "F" "F" "F"
[481] "F" "F" "M" "M" "M" "F" "F" "M" "M" "M" "F" "M" "M" "M" "M" "F" "F" "M" "F" "F"
[501] "F" "F" "F" "F" "M" "M" "M" "F" "M" "M" "M" "M" "F" "M" "F" "F" "F" "M" "M" "M"
[521] "M" "F" "F" "M" "F" "F" "F" "F" "M" "M" "M" "M" "M" "F" "F" "M" "M" "M" "F" "M"
[541] "M" "F" "M" "M" "F" "M" "M" "F" "M" "F" "M" "F" "M" "M" "F" "M" "M" "F" "M" "M"
[561] "F" "M" "F" "F" "M" "M" "F" "M" "F" "M" "M" "M" "M" "M" "M" "M" "M" "M" "M" "F"
[581] "F" "F" "F" "M" "F" "M" "M" "M" "M" "M" "M" "M" "M" "M" "F" "M" "M" "F" "F" "M"
[601] "F" "F" "M" "M" "M" "M" "M" "F" "M" "M" "F" "M" "F" "F" "F" "F" "F" "M" "F" "M"
[621] "M" "M" "M" "M" "M" "M" "F" "F" "F" "M" "F" "M" "F" "F" "M" "F" "F" "M" "F" "F"
[641] "M" "F" "F" "F" "M" "M" "M" "M" "F" "F" "F" "F" "F" "M" "M" "F" "F" "F" "F" "M"
[661] "M" "M" "M" "F" "M" "M" "M" "F" "F" "F" "M" "F" "F" "F" "M" "M" "M" "M" "F" "M"
[681] "F" "F" "M" "M" "F" "M" "F" "F" "M" "M" "F" "F" "F" "F" "F" "M" "M" "M" "F" "M"
[701] "M" "M" "F" "F" "M" "F" "M" "M" "F" "M" "F" "F" "M" "F" "F" "F" "F" "F" "M" "F"
[721] "F" "F" "F" "F" "M" "M" "F" "F" "M" "M" "M" "M" "M" "F" "M" "F" "M" "F" "F"
如您所见,数据包含“M”和“F”
我的困惑就是我这样做的时候:table(sells$Gender)
我得到了这个:
F
739
表示所有行都为F
,因为行数为739
我解决了,偶然这样做了:
sells$Gender <- factor(ifelse(sells$TitleCli=="MR","M","F"))
在做了那个事故解决方案之后,我做了这个:table(sells$Gender)
,结果是预期的
F M
413 326
我的问题是应用factor
之前无效的原因以及为什么它在它之后起作用
由于