我使用的是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
Run Code Online (Sandbox Code Playgroud)
max_size_aggr <- 3 / 4
water_nonair <- structure(list(`Slump (in.)` = c("1 to 2", "3 to 4", "6 to 7",
"Approximate amount of …
Run Code Online (Sandbox Code Playgroud) 我想到子集的消息(下面),以创建新闻2(下文),这将仅包括行/列,其中ABS(值)中的每个元素的消息 > 0.01.
以下是我尝试过的代码:
gr <- data.frame(which(abs(news[, 1:ncol(news), with = FALSE]) > 0.01,
arr.ind = TRUE))
news2a <- news[gr$row, c(1, gr$col + 1L), with = FALSE]
news2a[, which(duplicated(names(news2a))) := NULL]
Run Code Online (Sandbox Code Playgroud)
上面的代码并不总是有效.注意:在实际数据集中,还有更多行和列.
# news
ID diff.jan diff.feb diff.mar diff.apr
1: 7 -2.998852570e-13 2.764079712e-13 -3.291735832e-13 0.000000000e+00
2: 8 1.010000000e-01 -3.717073578e-13 -6.575639966e-13 -2.100269646e-13
3: 10 0.000000000e+00 -3.973537519e-13 0.000000000e+00 0.000000000e+00
4: 47 0.000000000e+00 0.000000000e+00 0.000000000e+00 -2.371100404e-13
5: 50 0.000000000e+00 -2.281689276e-13 2.192820401e-13 -1.857449127e-13
6: 79 0.000000000e+00 4.031985405e-13 -3.981825179e-13 0.000000000e+00
7: …
Run Code Online (Sandbox Code Playgroud) 我有一个data.table命名机会,我试图为每个小于100的值添加32.
chance <- data.table(Fe = c(75, 50, 100, 60, 60, 50, 60, 100))
> chance
Fe
1: 75
2: 50
3: 100
4: 60
5: 60
6: 50
7: 60
8: 100
Run Code Online (Sandbox Code Playgroud)
以下代码是我尝试更改j中的某些行,其中我是真的:
# changing Fe
change <- "Fe"
for (col in change) set(chance, i = which(chance[[col]] < 100), j = col,
value = chance[[col]] + 32L) # Source 3
Run Code Online (Sandbox Code Playgroud)
这是结果错误:
# Warning message:
# In set(chance, i = which(chance[[col]] < 100), j = col, value =
# chance[[col]] + …
Run Code Online (Sandbox Code Playgroud)