I am trying to automatically get, in a loop, a column from a dataframe. It is important to get this column by '$' operator df$column
, because if I slice the df like this df[,c('column')]
then I don't get anymore a haven_labelled object.
Example df
:
colName colName2
a 1
b 2
c 3
d 4
I was trying get(paste0('df$', colName))
, but I am getting:
Error in get(paste0('df$', colName)) : object 'df$colName' not found
However, if I write: df$colName
in my console, the column is printed. So the problem is that get only identifies the df as an object, but not its columns. Do you know how can I solve this?
What I would like is to get the following vector: c('a','b','c','d')
, but it must keep its labels.
Thanks!