Im super new to R and programming in general, so my description of stuff may be a bit off. I'll try to be as clear as possible. I have a dataframe df.train that has hundreds of factor levels with obfuscated entries that may or may not be unique to their respective factor. I'm trying to get the row id of every observation that has at least one factor level below some given amount. So If there is an nice way to get this I would like that answer. if not, this is my current attempt at a solution and where I got stuck, with a sample dataframe:

structure(list(GKUhYLAE = structure(c(1L, 2L, 1L, 1L, 2L, 1L), .Label = c("DDOFi", 
"fVvMw"), class = "factor"), OnTaJkLa = structure(c(1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("LDyDX", "sbxXu"), class = "factor"), 
    SsZAZLma = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("fSMSz", 
    "Hltat"), class = "factor"), BMmgMRvd = structure(c(2L, 1L, 
    2L, 2L, 2L, 2L), .Label = c("IjEdt", "QZujc"), class = "factor"), 
    OMtioXZZ = c(3L, 21L, 30L, 21L, 12L, 12L), bIBQTaHw = structure(c(1L, 
    3L, 1L, 3L, 3L, 2L), .Label = c("ALZyK", "qqkkL", "wQABW"
    ), class = "factor")), row.names = 1013:1018, class = "data.frame")

​ which gives me the following table

      GKUhYLAE   OnTaJkLa   SsZAZLma    BMmgMRvd    OMtioXZZ    bIBQTaHw
1013  DDOFi      LDyDX      fSMSz       QZujc       3           ALZyK
1014  fVvMw      LDyDX      fSMSz       IjEdt       21          wQABW
1015  DDOFi      LDyDX      fSMSz       QZujc       30          ALZyK
1016  DDOFi      LDyDX      fSMSz       QZujc       21          wQABW
1017  fVvMw      LDyDX      fSMSz       QZujc       12          wQABW
1018  DDOFi      LDyDX      fSMSz       QZujc       12          qqkkL

​ I can run the following to figure out which factors have counts lower than a certain amount:

library (dplyr)
df.test.a = lapply( df.test[,!names( df.test) %in% c("id")], count)
df.test.freqcount <- as.data.frame(do.call(rbind,  df.test.a)) 
df.test.list = df.test.freqcount[which( df.test.freqcount$freq <2),]

This returns:

            x       freq
BMmgMRvd.1  IjEdt   1
OMtioXZZ.1  NA      1
OMtioXZZ.4  NA      1
bIBQTaHw.2  qqkkL   1 

(the left most column is the column name with a .x after it which I assume is the factor level). Here is where im stuck. I figured the best way to get what I want is to make a vector with entries true or 1 when that entry of my dataframe has at least one column with a factor that is in my list of flagged factor. I cant figure out how to do this or how to construct a suitable list of flagged factors. what I want to write is this:

df.test.freqcount[which( df.test.freqcount$freq <2),]$x

Since the names are not unique and im geting NA values I dont expect, instead of checking by $x i want to check by the column.factorlevel thats a hidden column on the left of the table I gave, if possible.

what I would like as output is:

1013    TRUE
1014    TRUE
1015    TRUE
1016    FALSE
1017    FALSE
1018    TRUE
  • Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you. – Ronak Shah Mar 19 at 1:52
  • @RonakShah Is there a problem with my question or something that I should include? I included what I thought was the relevant bits of the code with no external references other than to my database 'df.train' which is too big to make reproducable. As far as I know I am following the guidelines but If there is something I should add I am more than willing to. – mother brain Mar 19 at 2:16
  • You could add first 10 rows of your data (head(df, 10)) or if it has got sensitive information which you cannot share publicly then create a fake data which is similar to your original data and include your expected output based on that. – Ronak Shah Mar 19 at 2:23

Your Answer


Browse other questions tagged or ask your own question.