A very simple question. With a data frame like this:> n = c(2, 3, 5) > s = c("aa", "bb", "cc") > df = data.frame(n, s)I want df$s[1] or df[1,2], but how can I get rid of the extra line in the output about the factor levels:> df$s[1][1] aa Levels: aa bb cc Thanks, Gang
See the max.levels argument in ?print. I think this is what you're looking for. -- Robert Tirrell | rpt@stanford.edu | (607) 437-6532 Program in Biomedical Informatics | Butte Lab | Stanford University On Thu, Mar 10, 2011 at 13:35, Gang Chen <gangchen6@gmail.com> wrote:> n = c(2, 3, 5) > > s = c("aa", "bb", "cc") > > df = data.frame(n, s) >[[alternative HTML version deleted]]
try this:> n = c(2, 3, 5) > s = c("aa", "bb", "cc") > df = data.frame(n, s, stringsAsFactors = FALSE) > dfn s 1 2 aa 2 3 bb 3 5 cc> str(df)'data.frame': 3 obs. of 2 variables: $ n: num 2 3 5 $ s: chr "aa" "bb" "cc">On Thu, Mar 10, 2011 at 4:35 PM, Gang Chen <gangchen6 at gmail.com> wrote:> A very simple question. With a data frame like this: > >> n = c(2, 3, 5) >> s = c("aa", "bb", "cc") >> df = data.frame(n, s) > > I want df$s[1] or df[1,2], but how can I get rid of the extra line in > the output about the factor levels: > >> df$s[1] > [1] aa > Levels: aa bb cc > > Thanks, > Gang > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
Gang - It sounds like you want your character variables to be stored as character values, not factor values. If that's the case, use df = data.frame(n, s,stringsAsFactors=FALSE) If you want them to be factors, but not to display as factors, others have provided usable solutions. - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Thu, 10 Mar 2011, Gang Chen wrote:> A very simple question. With a data frame like this: > >> n = c(2, 3, 5) >> s = c("aa", "bb", "cc") >> df = data.frame(n, s) > > I want df$s[1] or df[1,2], but how can I get rid of the extra line in > the output about the factor levels: > >> df$s[1] > [1] aa > Levels: aa bb cc > > Thanks, > Gang > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >