[英]R looping matrix elements
我正在嘗試遍歷矩陣列。
date <- rbind("2000-01-01", "2000-01-02", "2000-01-03", "2000-01-04", "2000-01-05", "2000-01-06", "2000-01-07", "2000-01-08", "2000-01-09", "2000-01-10", "2000-01-11", "2000-01-12")
a1 <- rbind("0", "0", "0", "0", "6421", "41", "5667", "44", "1178", "0", "1070", "1")
b1 <- rbind("1", "1", "1", "1", "6421", "41", "5667", "44", "1178", "0", "1070", "1")
hb1 <- rbind("2", "2", "2", "2", "6421", "41", "5667", "44", "1178", "0", "1070", "1")
a2 <- rbind("0", "0", "0", "0", "6421", "41", "5667", "44", "1178", "0", "1070", "1")
b2 <- rbind("1", "1", "1", "1", "6421", "41", "5667", "44", "1178", "0", "1070", "1")
hb2 <- rbind("2", "2", "2", "2", "6421", "41", "5667", "44", "1178", "0", "1070", "1")
a3 <- rbind("0", "0", "0", "0", "6421", "41", "5667", "44", "1178", "0", "1070", "1")
b3 <- rbind("1", "1", "1", "1", "6421", "41", "5667", "44", "1178", "0", "1070", "1")
hb3 <- rbind("2", "2", "2", "2", "6421", "41", "5667", "44", "1178", "0", "1070", "1")
a4 <- rbind("0", "0", "0", "0", "6421", "41", "5667", "44", "1178", "0", "1070", "1")
b4 <- rbind("1", "1", "1", "1", "6421", "41", "5667", "44", "1178", "0", "1070", "1")
hb4 <- rbind("2", "2", "2", "2", "6421", "41", "5667", "44", "1178", "0", "1070", "1")
info_mat <- cbind(date, a1, b1, hb1, a2, b2, hb2, a3, b3, hb3, a4, b4, hb4)
print(info_mat)
我想計算每個變量月份之間的演化率 (V+1 - V)/V(從 1 月到 2 月,從 2 月到 3 月,...,對於 a1,...,hb4)並得到結果在我將命名為“evolution_matrix”的矩陣中
我嘗試了以下方法,但由於某種原因它不起作用。 請注意,我在這里表示我想為每個變量執行進化的事實。 我認為 i 是:進化(變量 a1 的 1 月到 2 月)=(2 月 a1 的值 - 1 月 a1 的值)/(1 月 a1 的值)。
我不知道如何 model 它因此我輸入了 i,但它沒有引用矩陣中的任何內容。
for(row in 1:nrow(info_mat)) {
for(col in 1:ncol(info_mat)) {
evolution[[i]] = (info_mat[i+1] - info_mat[i] )/info_mat[i]
print(evolution[[i]])
}
}
請幫忙!
為什么要使用矩陣? 您在矩陣中只有字符(字符串)變量,但您想將它們用作數字。 我認為 data.frame 是個好主意。
R package dplyr
has function lapply which can apply your function to each column and simplify the result by list. 但我們不想將“進化”function 應用於列日期。
evolution <- as.data.frame(info_mat)[, -1] %>%
lapply(function(x) {x = as.numeric(x); (x - lag(x)) / lag(x)}) %>%
as.data.frame()
在最后一行中,我將 list 轉換為 data.frame(為了漂亮的打印)。
但是我們忘記了“日期”列。 讓我們將它添加到我們的 data.frame 中。
evolution <- bind_cols(data.frame(date = date), evolution)
就這些。 但是,如果您想通過循環執行此操作,則可以使用以下代碼:
evolution <- matrix(NA, nrow(info_mat), ncol(info_mat))
evolution[, 1] <- date
for(row in 2:nrow(info_mat)) {
for(col in 2:ncol(info_mat)) {
evolution[row, col] = as.numeric(info_mat[row, col])/as.numeric(info_mat[row - 1, col]) - 1
}
}
關於您的代碼示例的評論:
i
並且不要使用變量row
和col
。evolution
變量的類型是什么?info_mat[i+1]
不是數字。 你不能在info_mat[i]
上划分它。info_mat[i]
是什么意思? 是的, info_mat[row, col]
等於info_mat[(col - 1)* 12 (number of rows) + row]
但info_mat[i]
和info_mat[i + 1]
可以在不同的列中。如果您想用您的數據創建 data.frame,請使用以下代碼:
df = data.frame(
data = c("2000-01-01", "2000-01-02", "2000-01-03", "2000-01-04", "2000-01-05", "2000-01-06", "2000-01-07", "2000-01-08", "2000-01-09", "2000-01-10", "2000-01-11", "2000-01-12"),
a1 = c(0, 0, 0, 0, 6421, 41, 5667, 44, 1178, 0, 1070, 1),
b1 = c(1, 1, 1, 1, 6421, 41, 5667, 44, 1178, 0, 1070, 1)
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.