Return 个分类为逻辑的列号

Return column numbers that are classified as logical

我正在尝试编写一个函数,它将 运行 仅在我的数据框中的逻辑列上。为此,我首先尝试在我的数据框中生成列号列表,这些列号被分类为 "logical."

对于数据帧 m,这里是我的数据分类方式:

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 ...
(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

如果你真的需要一个数字向量,那么你可以另外应用 which:

which(sapply(df, is.logical))
# c 
# 3