I was trying to generate a dialog box where a user can select elements to keep, however the item I want to keep is a character but it looks like R is giving them numeric values based on their alphabetical rank. Here's an example (I would prefer the list in the dialog box to be 1:Red, 2:Yellow, 3: Blue instead it's giving me 2, 3, 1):
library(dplyr)
library(tcltk)
library(utils)
x<-c(1,2,5)
y<-c(7,2.2,8)
c<-c("red", "yellow", "blue")
df<-as.data.frame(cbind(x,y,c),stringsasfactors=FALSE)
answer<-tk_select.list(df$c, preselect = NULL, multiple = TRUE,
title = "Select items to keep:")
selected_df<-filter(df, c %in% answer)
selected_df
We can create the
data.frame
asWith this, the output of
selected_df
isNOTE:
stringsasfactors
is not equal tostringsAsFactors
. So, thecharacter
columns remain asfactor
classAlso,
as.data.frame(cbind
, would have unnecessary issues ascbind
returns amatrix
andmatrix
can have only a singleclass
i.e. all the columns are converted tocharacter
class because the values in 'c' arecharacter
. When we useas.data.frame
withstringsAsFactors = FALSE
, it does createcharacter
class butnumeric
columns should remain numeric