3
我有一个数据帧,它有几列。我想在其中一列上运行factor()函数,比如名称my_col。 起初,我就是这么做的数据帧的列上运行因子()的错误
df[,"my_col"]<-factor((df[,"my_col"]))
它给了以下错误
Error: 'x' must be atomic for 'sort.list' Have you called 'sort' on a list?
上指的是similar question上,所以我的问题得到解决。
现在,如果不是第一种方法我尝试下面的代码,它完美而无须给予任何错误
df$"my_col"<-factor(df$"my_col")
为什么?通过df $ vec_name和df [,vec_name]访问列是否有区别?
更新:
str(df)
Classes 'tbl_df', 'tbl' and 'data.frame': 160 obs. of 8 variables:
$ area : int 1 1 1 1 1 1 1 1 1 1 ...
$ temp : int 1 1 1 1 1 1 1 1 1 1 ...
$ size : int 1 1 1 1 1 1 1 1 1 1 ...
$ storage : int 1 1 1 1 1 2 2 2 2 2 ...
$ my_col : int 1 2 3 4 5 1 2 3 4 5 ...
$ texture : num 2.9 2.3 2.5 2.1 1.9 1.8 2.6 3 2.2 2 ...
$ flavor : num 3.2 2.5 2.8 2.9 2.8 3 3.1 3 3.2 2.8 ...
$ moistness: num 3 2.6 2.8 2.4 2.2 1.7 2.4 2.9 2.5 1.9 ...