r - 如何在 R 环境中循环遍历编号数据帧。我必须在 R 中循环 22 个(可能是 22*6)数据帧

标签 r loops for-loop

我有 6 个不同的数据帧,每个数据帧计算一组文档之间的余弦相似度。我已经计算了余弦相似度,我只需要在六个中的每一个上提取正确的变量并保存它。执行此操作的代码如下所示:

# first I have to convert the model (calculating cosine similarity) into a dataframe. The model is a "Formal class textstal_simil" from quanteda
y_V_2 <- as.data.frame(as.matrix(y_V_2))

#then I label the reference documents column to "id"
y_V_2 <- cbind(id = rownames(y_V_2), y_V_2)

# because I have my variables of interest as columns ("DF", "EL", etc. are all political parties) # I change convert the dataframe from wide to long
y_V_2 <- y_V_2 %>% 
  pivot_longer(c("DF", "EL", "KF", "RV", "S", "SF", "V"), names_to = "party", values_to = "cos_sim")

# lastly, I filter the correct cosine similarity and save it in the final dataframe called: cos_sim_V_2
cos_sim_V_2 <- y_V_2 %>% 
  filter(party == "V")

不确定这是否有意义。最重要的是,我使用六个不同的数据框(每个数据框代表一个政党)来完成此操作。该行代码如下所示:

y_V_2 <- as.data.frame(as.matrix(y_V_2))
y_S_2 <- as.data.frame(as.matrix(y_S_2))
y_EL_2 <- as.data.frame(as.matrix(y_EL_2))
y_SF_2 <- as.data.frame(as.matrix(y_SF_2))
y_DF_2 <- as.data.frame(as.matrix(y_DF_2))
y_KF_2 <- as.data.frame(as.matrix(y_KF_2))
y_RV_2 <- as.data.frame(as.matrix(y_RV_2))


y_V_2 <- cbind(id = rownames(y_V_2), y_V_2)
y_S_2 <- cbind(id = rownames(y_S_2), y_S_2)
y_EL_2 <- cbind(id = rownames(y_EL_2), y_EL_2)
y_SF_2 <- cbind(id = rownames(y_SF_2), y_SF_2)
y_DF_2 <- cbind(id = rownames(y_DF_2), y_DF_2)
y_KF_2 <- cbind(id = rownames(y_KF_2), y_KF_2)
y_RV_2 <- cbind(id = rownames(y_RV_2), y_RV_2)

y_V_2 <- y_V_2 %>% 
  pivot_longer(c("DF", "EL", "KF", "RV", "S", "SF", "V"), names_to = "party", values_to = "cos_sim")
y_S_2 <- y_S_2 %>% 
  pivot_longer(c("DF", "EL", "KF", "RV", "S", "SF", "V"), names_to = "party", values_to = "cos_sim")
y_EL_2 <- y_EL_2 %>% 
  pivot_longer(c("DF", "EL", "KF", "RV", "S", "SF", "V"), names_to = "party", values_to = "cos_sim")
y_SF_2 <- y_SF_2 %>% 
  pivot_longer(c("DF", "EL", "KF", "RV", "S", "SF", "V"), names_to = "party", values_to = "cos_sim")
y_DF_2 <- y_DF_2 %>% 
  pivot_longer(c("DF", "EL", "KF", "RV", "S", "SF", "V"), names_to = "party", values_to = "cos_sim")
y_KF_2 <- y_KF_2 %>% 
  pivot_longer(c("DF", "EL", "KF", "RV", "S", "SF", "V"), names_to = "party", values_to = "cos_sim")
y_RV_2 <- y_RV_2 %>% 
  pivot_longer(c("DF", "EL", "KF", "RV", "S", "SF", "V"), names_to = "party", values_to = "cos_sim")

cos_sim_V_2 <- y_V_2 %>% 
  filter(party == "V")
cos_sim_S_2 <- y_S_2 %>% 
  filter(party == "S")
cos_sim_EL_2 <- y_EL_2 %>% 
  filter(party == "EL")
cos_sim_SF_2 <- y_SF_2 %>% 
  filter(party == "SF")
cos_sim_DF_2 <- y_DF_2 %>% 
  filter(party == "DF")
cos_sim_KF_2 <- y_KF_2 %>% 
  filter(party == "KF")
cos_sim_RV_2 <- y_RV_2 %>% 
  filter(party == "RV")

现在,我真正想做的是:这六个数据帧适用于“2”年(因此每个数据帧末尾都有 2)。我实际上已经有22年的兴趣了。因此,我需要为 6 个参与方(第 1 方:y_V_2、y_V_3、y_V_4 等)执行整个操作 22 次。有什么办法可以循环这个吗?

我尝试过以下方法:

time <- 2:22

for (i in time){
  
  y_V_[[i]] <- as.data.frame(as.matrix(y_V_[[i]]))
  
  
  
  y_V_[[i]] <- cbind(id = rownames(y_V_[[i]]), y_V_[[i]])
  
  
  y_V_[[i]] <- y_V_[[i]] %>% 
    pivot_longer(c("DF", "EL", "KF", "RV", "S", "SF", "V"), names_to = "party", values_to = "cos_sim")
  
  y_V_[[i]] <- y_V_[[i]] %>% 
    filter(party == "V")
  
}

但是它不起作用。这样做的正确方法是什么?

如果有帮助,这就是数据帧的结构,一旦我将“formal_class textstal_simil”转换为数据帧:y_V_2 <- as.data.frame(as.matrix(y_V_2))

dput(head(y_V_2))

structure(list(DF = c(0.23499916674957, 0.16697708727056, 0.26998882552819, 
0.11989777626359, 0.28145930377199, 0.15959668959184), EL = c(0.23595981215221, 
0.18359709428329, 0.28810481269376, 0.13263861987521, 0.25331537435773, 
0.18167733395369), KF = c(0.20936950007655, 0.18252467175417, 
0.26042704505428, 0.14266913827392, 0.20023284784432, 0.18992935664409
), RV = c(0.2046697473122, 0.24951432279883, 0.24766480258903, 
0.11242986749057, 0.23958714529124, 0.16084468614859), S = c(0.24270069472492, 
0.18741729570808, 0.29014329186024, 0.14733535217516, 0.27150818619494, 
0.18979023415197), SF = c(0.23561869890038, 0.17927679461636, 
0.29403349472473, 0.15269893065285, 0.2559026802251, 0.17742356519735
), V = c(0.31302795687125, 0.2765158096593, 0.41588664999413, 
0.21090507950169, 0.34787076982177, 0.2583219375177)), row.names = c("Anders Fogh Rasmussen", 
"Anders Mølgaard", "Birthe Rønn Hornbech", "Bodil Thrane", "Charlotte Antonsen", 
"Christian Mejdahl"), class = "data.frame")

额外问题(但不是绝对必要):我可以将 22 年循环与 6 个不同方循环结合起来吗?这样我就只需要写原来的6行代码了。然后循环将通过各方(V、S、EL、SF、DF、KF、RV)以及各方的 22 年。

最佳答案

您可以使用get(object_name)按名称获取对象

for (i in time) {
  df <- get(paste0("y_V_", i))
}

将获取数据帧 y_V_{i},其中 i 是时间索引。 您也可以写这封信:

for (i in time) {
  for (l in letter_vector) {
    df <- get(paste0("y_", l, "_", i))
  }
}

将把 y_{l}_{i} 写入 df,因为它们都存在。这取决于你


编辑:使用分配写入粘贴的对象名称

for (i in time) {
  for (l in letter_vector) {
    df <- get(paste0("y_", l, "_", i))
    assign(paste0("df_", l, "_", i), df)
  }
}

第二次编辑。您可以将数据帧写入列表:

# first initialize the list
list_with_dfs <- list()

for (i in time) {
  for (l in letter_vector) {
    df <- get(paste0("y_", l, "_", i))
    assign(paste0("df_", l, "_", i), df)

    # Then write to the list
    list_with_dfs[[length(list_with_dfs) +  1]] <- get(paste0("df_", l, "_", i))

    # Or just use the df 
    list_with_dfs[[length(list_with_dfs) +  1]] <- df
  }
}

关于r - 如何在 R 环境中循环遍历编号数据帧。我必须在 R 中循环 22 个(可能是 22*6)数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70200199/

相关文章:

Python 相当于 R 的 Pandas 数据框列表

r - 将来自多个数据帧的内容绘制到单个 ggplot2 表面中

loops - SAS中嵌套道循环的解释

javascript - 如何改进这个 session 存储命名系统/for循环?

javascript - 检查数组是否包含给定元素

r - 如何在 R 中打乱数据帧条目

r - 我可以在子集的 Cox PH 中*重新定义因子的引用水平吗?

Ruby 哨兵循环未按预期工作(七周七种语言)

Swift:如何在不使用连接或高阶函数的情况下逐层打印树?

javascript - 按键排序数组或 : Why is my for loop executing out of order?