Hi All, I am using factors in a study for the social sciences. I discovered the following: -- cut -- library(dplyr) test1 <- c(rep(1, 4), rep(0, 6)) d_test1 <- data.frame(test) test2 <- factor(test1) d_test2 <- data.frame(test2) test3 <- factor(test1, levels = c(0, 1), labels = c("WITHOUT Contact", "WITH Contact")) d_test3 <- data.frame(test3) d_test1 %>% filter(test1 == 0) # works OK d_test2 %>% filter(test2 == 0) # works OK d_test3 %>% filter(test3 == 0) # does not work, why? myf <- function(ds) { print(levels(ds$test3)) print(labels(ds$test3)) print(as.numeric(ds$test3)) print(as.character(ds$test3)) } # This showsthat it is not possible to access the original # values which were the basis to build the factor: myf(d_test3) -- cut -- Why is it not possible to use a factor with labels for filtering with the original values? Is there a data structure that works like a factor but gives also access to the original values? Kind regards Georg
That's easy! First> str(test3)Factor w/ 2 levels "WITHOUT Contact",..: 2 2 2 2 1 1 1 1 1 1 tells you that the internal values are 1 and 2, and the labels are "WITHOUT Contact" and "WITH Contact". If you read the help page for factor() you'll see this: levels: an optional vector of the values (as character strings) that ?x? might have taken. The default is the unique set of values taken by ?as.character(x)?, sorted into increasing order _of ?x?_. Note that this set can be specified as smaller than ?sort(unique(x))?. labels: _either_ an optional character vector of (unique) labels for the levels (in the same order as ?levels? after removing those in ?exclude?), _or_ a character string of length 1. So, when you create test3 you say that test can take values 0 and 1, and these should be labelled as "WITHOUT Contact" and "WITH Contact". So R internally codes "1" as 1 and "0" as 2 (internally R codes factors as integers, which can be both useful and dangerous), and then gives them labels "WITHOUT Contact" and "WITH Contact". It now doesn't care that they were 1 and 0, because you've told it to change the labels. If you want to filter by the original values, then don't change the labels (or at least not until after you've filtered by the original labels), or convert the filter to the new labels. You're asking for a data structure with two sets of labels, which sounds odd in general. Bob On 9 May 2017 at 12:12, <G.Maubach at weinwolf.de> wrote:> Hi All, > > I am using factors in a study for the social sciences. > > I discovered the following: > > -- cut -- > > library(dplyr) > > test1 <- c(rep(1, 4), rep(0, 6)) > d_test1 <- data.frame(test) > > test2 <- factor(test1) > d_test2 <- data.frame(test2) > > test3 <- factor(test1, > levels = c(0, 1), > labels = c("WITHOUT Contact", "WITH Contact")) > d_test3 <- data.frame(test3) > > d_test1 %>% filter(test1 == 0) # works OK > d_test2 %>% filter(test2 == 0) # works OK > d_test3 %>% filter(test3 == 0) # does not work, why? > > myf <- function(ds) { > print(levels(ds$test3)) > print(labels(ds$test3)) > print(as.numeric(ds$test3)) > print(as.character(ds$test3)) > } > > # This showsthat it is not possible to access the original > # values which were the basis to build the factor: > myf(d_test3) > > -- cut -- > > Why is it not possible to use a factor with labels for filtering with the > original values? > Is there a data structure that works like a factor but gives also access > to the original values? > > Kind regards > > Georg > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.-- Bob O'Hara NOTE NEW ADDRESS!!! Institutt for matematiske fag NTNU 7491 Trondheim Norway Mobile: +49 1515 888 5440 Journal of Negative Results - EEB: www.jnr-eeb.org
Hi Bob, many thanks for your reply. I have read the documentation. In my current project I use "item batteries" for dimensions of touchpoints which are rated by our customers. I wrote functions to analyse them. If I create a factor before filtering and analysing I lose the original values of the variable. If I use the original variable for filtering and analysis I might happen that for some dimensions values were not selected. This means they are not NA but none of the respondents chose "4" for instance on a scale from 1 to 6. That means that creating a factor from the analysed data with the complete scale (1:6) fails due the different vector length (amount of remaining unique values in the analysis vs values in the scale). As I have a function doing the analysis I am looking for a way to make my function robust to such circumstances and be able to use it to analyse all "item batteries". Thus my question. I believe my findings are not odd. Maybe there is a way dealing with that kind of problems in R and I am eager to learn how it can be solved using R. What would you suggest? Kind regards Georg Von: "Bob O'Hara" <rni.boh at gmail.com> An: G.Maubach at weinwolf.de, Kopie: r-help <r-help at r-project.org> Datum: 09.05.2017 12:26 Betreff: Re: [R] Factors and Alternatives That's easy! First> str(test3)Factor w/ 2 levels "WITHOUT Contact",..: 2 2 2 2 1 1 1 1 1 1 tells you that the internal values are 1 and 2, and the labels are "WITHOUT Contact" and "WITH Contact". If you read the help page for factor() you'll see this: levels: an optional vector of the values (as character strings) that ?x? might have taken. The default is the unique set of values taken by ?as.character(x)?, sorted into increasing order _of ?x?_. Note that this set can be specified as smaller than ?sort(unique(x))?. labels: _either_ an optional character vector of (unique) labels for the levels (in the same order as ?levels? after removing those in ?exclude?), _or_ a character string of length 1. So, when you create test3 you say that test can take values 0 and 1, and these should be labelled as "WITHOUT Contact" and "WITH Contact". So R internally codes "1" as 1 and "0" as 2 (internally R codes factors as integers, which can be both useful and dangerous), and then gives them labels "WITHOUT Contact" and "WITH Contact". It now doesn't care that they were 1 and 0, because you've told it to change the labels. If you want to filter by the original values, then don't change the labels (or at least not until after you've filtered by the original labels), or convert the filter to the new labels. You're asking for a data structure with two sets of labels, which sounds odd in general. Bob On 9 May 2017 at 12:12, <G.Maubach at weinwolf.de> wrote:> Hi All, > > I am using factors in a study for the social sciences. > > I discovered the following: > > -- cut -- > > library(dplyr) > > test1 <- c(rep(1, 4), rep(0, 6)) > d_test1 <- data.frame(test) > > test2 <- factor(test1) > d_test2 <- data.frame(test2) > > test3 <- factor(test1, > levels = c(0, 1), > labels = c("WITHOUT Contact", "WITH Contact")) > d_test3 <- data.frame(test3) > > d_test1 %>% filter(test1 == 0) # works OK > d_test2 %>% filter(test2 == 0) # works OK > d_test3 %>% filter(test3 == 0) # does not work, why? > > myf <- function(ds) { > print(levels(ds$test3)) > print(labels(ds$test3)) > print(as.numeric(ds$test3)) > print(as.character(ds$test3)) > } > > # This showsthat it is not possible to access the original > # values which were the basis to build the factor: > myf(d_test3) > > -- cut -- > > Why is it not possible to use a factor with labels for filtering withthe> original values? > Is there a data structure that works like a factor but gives also access > to the original values? > > Kind regards > > Georg > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.-- Bob O'Hara NOTE NEW ADDRESS!!! Institutt for matematiske fag NTNU 7491 Trondheim Norway Mobile: +49 1515 888 5440 Journal of Negative Results - EEB: www.jnr-eeb.org
Inline...> On 9 May 2017, at 12:12 , G.Maubach at weinwolf.de wrote: > > Hi All, > > I am using factors in a study for the social sciences. > > I discovered the following: > > -- cut -- > > library(dplyr) > > test1 <- c(rep(1, 4), rep(0, 6)) > d_test1 <- data.frame(test) > > test2 <- factor(test1) > d_test2 <- data.frame(test2) > > test3 <- factor(test1, > levels = c(0, 1), > labels = c("WITHOUT Contact", "WITH Contact")) > d_test3 <- data.frame(test3) > > d_test1 %>% filter(test1 == 0) # works OK > d_test2 %>% filter(test2 == 0) # works OK > d_test3 %>% filter(test3 == 0) # does not work, why? >test3 does not have a level 0. You want test3 == "WITHOUT Contact" Notice that once test3 is created, the input levels are lost, and thus "test3 == 0" becomes meaningless. -pd> myf <- function(ds) { > print(levels(ds$test3)) > print(labels(ds$test3)) > print(as.numeric(ds$test3)) > print(as.character(ds$test3)) > } > > # This showsthat it is not possible to access the original > # values which were the basis to build the factor: > myf(d_test3) > > -- cut -- > > Why is it not possible to use a factor with labels for filtering with the > original values? > Is there a data structure that works like a factor but gives also access > to the original values? > > Kind regards > > Georg > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com