[R] Factors and Alternatives

G.Maubach at weinwolf.de G.Maubach at weinwolf.de
Tue May 9 12:12:42 CEST 2017


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



More information about the R-help mailing list