重复循环并在 R 中添加列

Repeating loop and adding columns in R

我正在尝试构建一个 R 代码,它将使用我的循环 运行 它 20 次。每次我想在现有数据框中添加一列。在这里,我通过添加代码 3 次来尝试它,但我觉得必须有一种更简单的方法来自动化它。我非常感谢任何帮助。

我的原始数据文件(名为 "igel")包含两列("Year" 和 "Grid")和 1096 行。通过循环,我从 "Grid" 列中选择一个随机数,并检查它之前是否被选择过。如果是,则将 0 添加到新列,否则将添加 1。

这里是代码:

a <- data.frame(matrix(ncol = 2, nrow = 0))
x <- c("number", "count")
colnames(a) <- x

for (i in 1:1096) {
num_i <- sample(igel$Grid, 1)
count_i <- c(if (num_i %in% a$number == TRUE) {0} else {1})
a<-a %>% add_row(number = num_i, count = count_i)
}

b <- data.frame(matrix(ncol = 2, nrow = 0))
x <- c("number", "count")
colnames(b) <- x

for (i in 1:1096) {
num_i <- sample(igel$Grid, 1)
count_i <- c(if (num_i %in% b$number == TRUE) {0} else {1})
b<-b %>% add_row(number = num_i, count = count_i)
}

c <- data.frame(matrix(ncol = 2, nrow = 0))
x <- c("number", "count")
colnames(c) <- x

for (i in 1:1096) {
num_i <- sample(igel$Grid, 1)
count_i <- c(if (num_i %in% c$number == TRUE) {0} else {1})
c<-c %>% add_row(number = num_i, count = count_i)
}

df.total<- cbind(a$count,b$count, c$count) 

考虑 sapply 甚至它的包装器 replicate 并在向量计算中分别计算 numbercount而不是逐行循环增长对象。

# RUNS 3 SAMPLES OF igel$Grid 1,096 TIMES (ADJUST 3 TO ANY POSITIVE INT LIKE 20)
grid_number <- data.frame(replicate(3, replicate(1096, sample(igel$Grid, 1))))

# RUNS ACROSS 3 COLUMNS TO CHECK CURRENT ROW VALUE IS INCLUDED FOR ALL VALUES BEFORE ROW
grid_count <- sapply(grid_number, function(col)
                       sapply(seq_along(col), function(i) 
                                 ifelse(col[i] %in% col[1:(i-1)], 0, 1)
                             )
                     )

虽然上面没有完全重现你的输出,df.total(矩阵而不是数据框),由于迭代中的随机抽样,两者保持相似的结构:

dim(df.total)
# [1] 1096    3

dim(grid_count)
# [1] 1096    3

尽量避免遍历行。很少有必要,如果有的话。这是一种方法(将 n 替换为 1096,将 elem 替换为 igel$Grid):

n = 20
elem = 1:5
df.total = list()
for  (i in 1:5) {
    a = data.frame(number = sample(elem, n, replace=TRUE))
    a$count = as.numeric(duplicated(a$number))
    df.total[[i]] = a
}

df.total = as.data.frame(df.total)
df.total
##    number count number.1 count.1 number.2 count.2 number.3 count.3 number.4 count.4
## 1       4     0        2       0        5       0        4       0        1       0
## 2       3     0        5       0        3       0        4       1        3       0
## 3       5     0        3       0        4       0        2       0        4       0
## 4       5     1        1       0        2       0        5       0        3       1
## 5       2     0        4       0        2       1        5       1        5       0
## 6       4     1        2       1        2       1        5       1        5       1
## 7       5     1        1       1        3       1        2       1        4       1
## 8       5     1        2       1        5       1        5       1        4       1
## 9       2     1        1       1        1       0        1       0        1       1
## 10      3     1        1       1        5       1        4       1        1       1
## 11      5     1        3       1        1       1        3       0        5       1
## 12      2     1        1       1        2       1        5       1        1       1
## 13      3     1        5       1        4       1        5       1        4       1
## 14      1     0        4       1        2       1        4       1        1       1
## 15      4     1        4       1        2       1        5       1        1       1
## 16      4     1        2       1        5       1        2       1        5       1
## 17      3     1        1       1        1       1        3       1        2       0
## 18      2     1        2       1        2       1        2       1        2       1
## 19      2     1        3       1        1       1        2       1        1       1
## 20      1     1        3       1        2       1        1       1        3       1