R中For循环更新的动态变量
Dynamic Variables Updated by For Loop in R
我有一个看起来像这样的矢量:
FirstVector <- c("Dog Fox Funny"
, "Dog Dolphin Angry"
, "Cat Catfish Bored"
, "Fish Clam Startled"
, "Lion Tiger Startled"
, "Cat Catfish Bored"
)
我需要为每两个条目生成一系列词频表,看起来像这样(以数据矩阵的形式):
Dataframe1
angry dog dolphin fox funny
[1,] 0 1 0 1 1
[2,] 1 1 1 0 0
Dataframe2
bored cat catfish clam fish startled
[1,] 1 1 1 0 0 0
[2,] 0 0 0 1 1 1
Dataframe3
bored cat catfish lion startled tiger
[1,] 0 0 0 1 1 1
[2,] 1 1 1 0 0 0
我可以使用以下语法创建表格。但是我需要将它们保存为矩阵。因此,从语法上我需要创建 dataframe1、dataframe2 和 dataframe3。
beginvar <- 1
endvar <- 2
SecondVector <- FirstVector[c(beginvar:endvar)]
for(i in 1:3) {
df <- tibble(id = seq_along(SecondVector), text = SecondVector)
a <- as.matrix.data.frame(table(unnest_tokens(df, word, text)))
b <- colnames(table(unnest_tokens(df, word, text)))
colnames(a) <- b
beginvar <- beginvar + 2
endvar <- endvar + 2
}
* 我的实际数据集要大得多。为了处理目的,我把它分成几块 *
我们可以在循环中创建 SecondVector
,同时初始化 list
来存储输出
beginvar <- 1
endvar <- 2
lst1 <- vector('list', 3)
for(i in 1:3) {
SecondVector <- FirstVector[c(beginvar:endvar)]
df <- tibble(id = seq_along(SecondVector), text = SecondVector)
a <- as.matrix.data.frame(table(unnest_tokens(df, word, text)))
b <- colnames(table(unnest_tokens(df, word, text)))
colnames(a) <- b
lst1[[i]] <- a
beginvar <- beginvar + 2
endvar <- endvar + 2
}
-输出
lst1
#[[1]]
# angry dog dolphin fox funny
#[1,] 0 1 0 1 1
#[2,] 1 1 1 0 0
#[[2]]
# bored cat catfish clam fish startled
#[1,] 1 1 1 0 0 0
#[2,] 0 0 0 1 1 1
#[[3]]
# bored cat catfish lion startled tiger
#[1,] 0 0 0 1 1 1
#[2,] 1 1 1 0 0 0
或使用while
循环
beginvar <- 1
endvar <- 2
lst1 <- list()
flag <- TRUE
i <- 1
while(flag){
SecondVector <- FirstVector[c(beginvar:endvar)]
df <- tibble(id = seq_along(SecondVector), text = SecondVector)
a <- as.matrix.data.frame(table(unnest_tokens(df, word, text)))
b <- colnames(table(unnest_tokens(df, word, text)))
colnames(a) <- b
lst1[[i]] <- a
if(endvar >= length(FirstVector)){
flag <- FALSE
} else
beginvar <- beginvar + 2
endvar <- endvar + 2
i <- i + 1
}
如果我们需要在全局环境中创建多个对象
beginvar <- 1
endvar <- 2
flag <- TRUE
i <- 1
while(flag){
SecondVector <- FirstVector[c(beginvar:endvar)]
df <- tibble(id = seq_along(SecondVector), text = SecondVector)
a <- as.matrix.data.frame(table(unnest_tokens(df, word, text)))
b <- colnames(table(unnest_tokens(df, word, text)))
colnames(a) <- b
assign(paste0('matrix', i), a)
if(endvar >= length(FirstVector)){
flag <- FALSE
} else
beginvar <- beginvar + 2
endvar <- endvar + 2
i <- i + 1
}
matrix1
# angry dog dolphin fox funny
#[1,] 0 1 0 1 1
#[2,] 1 1 1 0 0
matrix2
# bored cat catfish clam fish startled
#[1,] 1 1 1 0 0 0
#[2,] 0 0 0 1 1 1
matrix3
# bored cat catfish lion startled tiger
#[1,] 0 0 0 1 1 1
#[2,] 1 1 1 0 0 0
我有一个看起来像这样的矢量:
FirstVector <- c("Dog Fox Funny"
, "Dog Dolphin Angry"
, "Cat Catfish Bored"
, "Fish Clam Startled"
, "Lion Tiger Startled"
, "Cat Catfish Bored"
)
我需要为每两个条目生成一系列词频表,看起来像这样(以数据矩阵的形式):
Dataframe1
angry dog dolphin fox funny
[1,] 0 1 0 1 1
[2,] 1 1 1 0 0
Dataframe2
bored cat catfish clam fish startled
[1,] 1 1 1 0 0 0
[2,] 0 0 0 1 1 1
Dataframe3
bored cat catfish lion startled tiger
[1,] 0 0 0 1 1 1
[2,] 1 1 1 0 0 0
我可以使用以下语法创建表格。但是我需要将它们保存为矩阵。因此,从语法上我需要创建 dataframe1、dataframe2 和 dataframe3。
beginvar <- 1
endvar <- 2
SecondVector <- FirstVector[c(beginvar:endvar)]
for(i in 1:3) {
df <- tibble(id = seq_along(SecondVector), text = SecondVector)
a <- as.matrix.data.frame(table(unnest_tokens(df, word, text)))
b <- colnames(table(unnest_tokens(df, word, text)))
colnames(a) <- b
beginvar <- beginvar + 2
endvar <- endvar + 2
}
* 我的实际数据集要大得多。为了处理目的,我把它分成几块 *
我们可以在循环中创建 SecondVector
,同时初始化 list
来存储输出
beginvar <- 1
endvar <- 2
lst1 <- vector('list', 3)
for(i in 1:3) {
SecondVector <- FirstVector[c(beginvar:endvar)]
df <- tibble(id = seq_along(SecondVector), text = SecondVector)
a <- as.matrix.data.frame(table(unnest_tokens(df, word, text)))
b <- colnames(table(unnest_tokens(df, word, text)))
colnames(a) <- b
lst1[[i]] <- a
beginvar <- beginvar + 2
endvar <- endvar + 2
}
-输出
lst1
#[[1]]
# angry dog dolphin fox funny
#[1,] 0 1 0 1 1
#[2,] 1 1 1 0 0
#[[2]]
# bored cat catfish clam fish startled
#[1,] 1 1 1 0 0 0
#[2,] 0 0 0 1 1 1
#[[3]]
# bored cat catfish lion startled tiger
#[1,] 0 0 0 1 1 1
#[2,] 1 1 1 0 0 0
或使用while
循环
beginvar <- 1
endvar <- 2
lst1 <- list()
flag <- TRUE
i <- 1
while(flag){
SecondVector <- FirstVector[c(beginvar:endvar)]
df <- tibble(id = seq_along(SecondVector), text = SecondVector)
a <- as.matrix.data.frame(table(unnest_tokens(df, word, text)))
b <- colnames(table(unnest_tokens(df, word, text)))
colnames(a) <- b
lst1[[i]] <- a
if(endvar >= length(FirstVector)){
flag <- FALSE
} else
beginvar <- beginvar + 2
endvar <- endvar + 2
i <- i + 1
}
如果我们需要在全局环境中创建多个对象
beginvar <- 1
endvar <- 2
flag <- TRUE
i <- 1
while(flag){
SecondVector <- FirstVector[c(beginvar:endvar)]
df <- tibble(id = seq_along(SecondVector), text = SecondVector)
a <- as.matrix.data.frame(table(unnest_tokens(df, word, text)))
b <- colnames(table(unnest_tokens(df, word, text)))
colnames(a) <- b
assign(paste0('matrix', i), a)
if(endvar >= length(FirstVector)){
flag <- FALSE
} else
beginvar <- beginvar + 2
endvar <- endvar + 2
i <- i + 1
}
matrix1
# angry dog dolphin fox funny
#[1,] 0 1 0 1 1
#[2,] 1 1 1 0 0
matrix2
# bored cat catfish clam fish startled
#[1,] 1 1 1 0 0 0
#[2,] 0 0 0 1 1 1
matrix3
# bored cat catfish lion startled tiger
#[1,] 0 0 0 1 1 1
#[2,] 1 1 1 0 0 0