I am using data.table
1.10.0.
# install.packages("install.load") # install in order to use the load_package function
install.load::load_package("data.table", "gsubfn", "fpCompare")
# function to convert from fractions and numeric numbers to numeric (decimal)
# Source 1 begins
to_numeric <- function(n) {
p <- c(if (length(n) == 2) 0, as.numeric(n), 0:1)
p[1] + p[2] / p[3]
}
# Source 1 ends
Source 1 is Convert a character vector of mixed numbers, fractions, and integers to numeric
max_size_aggr <- 3 / 4
water_nonair <- structure(list(`Slump (in.)` = c("1 to 2", "3 to 4", "6 to 7",
"Approximate amount of entrapped air in nonair- entrained concrete (%)"), `3/8 in.` =
c(350, 385, 410, 3), `1/2 in.` = c(335, 365, 385, 2.5), `3/4 in.` = c(315, 340, 360, 2),
`1 in.` = c(300, 325, 340, 1.5), `1 1/2 in.` = c(275, 300, 315, 1), `2 in.` =
c(260, 285, 300, 0.5), `3 in.` = c(220, 245, 270, 0.3), `6 in.` = c(190, 210, NA, 0.2)),
.Names = c("Slump (in.)", "3/8 in.", "1/2 in.",
"3/4 in.", "1 in.", "1 1/2 in.", "2 in.", "3 in.", "6 in."), row.names = c(NA, -4L),
class = c("data.table", "data.frame"))
setnames(water_nonair, c("Slump (in.)", "3/8 in.", "1/2 in.", "3/4 in.", "1 in.",
"1 1/2 in.", "2 in.", "3 in.", "6 in."))
water_nonair_col_numeric <- gsub(" in.", "", colnames(water_nonair)[2:ncol(water_nonair)])
water_nonair_col_numeric <- sapply(strapplyc(water_nonair_col_numeric, "\\d+"), to_numeric)
# Source 1
New way (data.table
1.10.0)
water_nonair_column <- which(water_nonair_col_numeric %==% max_size_aggr)+1L
# [1] 4
water_nonair[2, water_nonair_column][[1]]
# [1] 4
Why does the following work when I call out the column index, but the above, also, with a value of 4 does not work?
water_nonair[2, 4][[1]]
# [1] 340
Old way (data.table
1.9.6)
water_nonair[2, which(water_nonair_col_numeric %==% max_size_aggr)+1L, with = FALSE][[1]]
# [1] 340
I removed the with = FALSE
from the function after reading the data.table
news after the release of version 1.9.8.