在 sapply 中使用逻辑向量
Use a logical vector with sapply
我正在尝试使用逻辑向量 'tell' 应用哪些列在我的数据集中生成数字。
在我的数据中,有 NA,但所有变量都是数字或字符。我正在处理第一个完整的案例(下面是硬代码,但希望得到建议!)并根据字符串中的第一个字符是数字还是字母来制作逻辑向量。我想使用该逻辑向量来告诉 sapply 哪些列要设为数字。
#make data frame, this should return an all 'character' data frame
color <- c("red", "blue", "yellow")
number <- c(NA, 1, 3)
other.number <- c(4, 5, 7)
df <- cbind(color, number, other.number) %>% as.data.frame()
#get the first character of the variables in the first complete case
temp <- sapply(df, function(x) substr(x, 1, 1)) %>% as.data.frame() %>%
.[2,] %>% # hard code, this is the first 'complete case'
gather() %>%
#make the logical variable, which can be used as a vector
mutate(vec= ifelse(value %in% letters, FALSE, TRUE)) # apply this vector to sapply + as.numeric to the df
这是一个奇怪的情况,但如果您需要根据第一个元素转换数字列,那么一个想法是将其转换为数字。由于任何不是数字的元素都会 return NA
(如警告所述),您可以使用它来编制索引。例如,
ind <- sapply(na.omit(df), function(i) !is.na(as.numeric(i[1])))
Warning message:
In FUN(X[[i]], ...) : NAs introduced by coercion
ind
# color number other.number
# FALSE TRUE TRUE
df[ind] <- lapply(df[ind], as.numeric)
str(df)
#'data.frame': 3 obs. of 3 variables:
# $ color : chr "red" "blue" "yellow"
# $ number : num NA 1 3
# $ other.number: num 4 5 7
数据
dput(df)
structure(list(color = c("red", "blue", "yellow"), number = c(NA,
"1", "3"), other.number = c("4", "5", "7")), .Names = c("color",
"number", "other.number"), row.names = c(NA, -3L), class = "data.frame")
我正在尝试使用逻辑向量 'tell' 应用哪些列在我的数据集中生成数字。
在我的数据中,有 NA,但所有变量都是数字或字符。我正在处理第一个完整的案例(下面是硬代码,但希望得到建议!)并根据字符串中的第一个字符是数字还是字母来制作逻辑向量。我想使用该逻辑向量来告诉 sapply 哪些列要设为数字。
#make data frame, this should return an all 'character' data frame
color <- c("red", "blue", "yellow")
number <- c(NA, 1, 3)
other.number <- c(4, 5, 7)
df <- cbind(color, number, other.number) %>% as.data.frame()
#get the first character of the variables in the first complete case
temp <- sapply(df, function(x) substr(x, 1, 1)) %>% as.data.frame() %>%
.[2,] %>% # hard code, this is the first 'complete case'
gather() %>%
#make the logical variable, which can be used as a vector
mutate(vec= ifelse(value %in% letters, FALSE, TRUE)) # apply this vector to sapply + as.numeric to the df
这是一个奇怪的情况,但如果您需要根据第一个元素转换数字列,那么一个想法是将其转换为数字。由于任何不是数字的元素都会 return NA
(如警告所述),您可以使用它来编制索引。例如,
ind <- sapply(na.omit(df), function(i) !is.na(as.numeric(i[1])))
Warning message: In FUN(X[[i]], ...) : NAs introduced by coercion
ind
# color number other.number
# FALSE TRUE TRUE
df[ind] <- lapply(df[ind], as.numeric)
str(df)
#'data.frame': 3 obs. of 3 variables:
# $ color : chr "red" "blue" "yellow"
# $ number : num NA 1 3
# $ other.number: num 4 5 7
数据
dput(df)
structure(list(color = c("red", "blue", "yellow"), number = c(NA,
"1", "3"), other.number = c("4", "5", "7")), .Names = c("color",
"number", "other.number"), row.names = c(NA, -3L), class = "data.frame")