I have a dataframe which has several columns. I want to run the factor() function on one of the columns, say name my_col. Initially I did it this way
df[,"my_col"]<-factor((df[,"my_col"]))
It gave the following error
Error: 'x' must be atomic for 'sort.list' Have you called 'sort' on a list?
On referring to a similar question on SO my problem was solved.
Now if instead of the first method I try the following code, it works perfectly without giving any error
df$"my_col"<-factor(df$"my_col")
Why's that? Is there a difference between accessing a column via df$vec_name and df[,vec_name]?
Update:
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 ...
See Question&Answers more detail:os