-1

I am trying to write a function that will run only on the logical columns in my dataframe. To do this I am first trying to generate a list of the column numbers in my data frame which are classified as "logical."

For dataframe m here are how my data is classified:

str(m)
'data.frame':   169 obs. of  42 variables:
 $ Municipality                  : chr  "Andover" "Ansonia" "Ashford" "Avon" ...
 $ Webpage_Link                  : chr  "http://andoverconnecticut.org/services-government/building-zoning/" "http://www.cityofansonia.com/content/2524/2582/default.aspx" "http://www.ashfordtownhall.org/government/land-use/building-department/" "http://www.avonct.gov/building-department" ...
 $ Webpage_LastUpdate            : chr  NA NA NA NA ...
 $ Researcher                    : chr  "DM" "DM" "LG" "LG" ...
 $ Collection.Date               : chr  "11/4/15" "11/4/15" "1/26/16" "1/26/16" ...
 $ Complete                      : logi  TRUE TRUE TRUE TRUE TRUE TRUE ...
 $ Application_BuildPermit       : logi  TRUE FALSE TRUE TRUE FALSE TRUE ...
 $ Application_SolarPermt        : logi  FALSE FALSE TRUE TRUE FALSE FALSE ...
 $ Contact_Name                  : logi  TRUE TRUE TRUE TRUE TRUE FALSE ...
 $ Contact_Address               : logi  TRUE TRUE TRUE TRUE TRUE TRUE ...
Danny
  • 554
  • 1
  • 6
  • 17
  • This question is pretty much the same: http://stackoverflow.com/q/5863097 You should probably resist the temptation to work with column numbers instead of column names, btw. – Frank Feb 16 '16 at 21:35

1 Answers1

2
(df <- data.frame(a = 1:3, b = letters[1:3], c = TRUE))
#   a b    c
# 1 1 a TRUE
# 2 2 b TRUE
# 3 3 c TRUE

sapply(df, is.logical)
#     a     b     c 
# FALSE FALSE  TRUE

If you really need a vector of numbers then you may additionally apply which:

which(sapply(df, is.logical))
# c 
# 3 
Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
  • @Danny, If this answer solves your problem consider accepting and upvoting it as well as some answers to your previous questions, see http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Julius Vainora Feb 16 '16 at 23:36