有没有办法在for循环中为数据框赋值?
Is there a way to assign values to a data frame in a for-loop?
我想使用 for 循环为数据帧赋值,该循环遍历数据帧中的不同行。
我试图将我需要的变量名分配给一些值,这些变量名在每次迭代中都会发生变化,然后将这些值放在一个数据框中,但没有用
banks <- read.csv("banks_csv.csv", sep = ";", stringsAsFactors = FALSE)
banks<-data.frame(banks)
for (row in 1:nrow(banks)){
bank = banks[row, "Company"]
reviews <- read.csv(paste(bank, "_cleaned.csv", sep = ""))
reviews$X = NULL
reviews_summary <- reviews %>%
group_by(Year_Quarter) %>%
summarise(rat_gen = mean(rat_general, na.rm = TRUE),
CV = mean(rat_CV, na.rm = TRUE),
WL = mean(rat_WL, na.rm = TRUE),
CO = mean(rat_CO, na.rm = TRUE),
CB = mean(rat_CB, na.rm = TRUE),
SM = mean(rat_SM, na.rm = TRUE))
nam <- paste("general_", bank, sep = "")
assign(nam, reviews_summary$rat_gen)
nam <- paste("CV_", bank, sep = "")
assign(nam, reviews_summary$CV)
nam <- paste("WL_", bank, sep = "")
assign(nam, reviews_summary$WL)
nam <- paste("CO_", bank, sep = "")
as.list(nam, reviews_summary$CO)
nam <- paste("CB_", bank, sep = "")
as.list(nam, reviews_summary$CB)
nam <- paste("SM_", bank, sep = "")
as.list(nam, reviews_summary$SM)
df <- data.frame(paste("general_", bank, sep = ""), paste("CV_", bank, sep = ""), paste("WL_", bank, sep = ""), paste("CO_", bank, sep = ""), paste("CB_", bank, sep = ""), paste("SM_", bank, sep = ""))
}
编辑:我希望只有一个 data.frame,其中每次迭代添加 6 列,而不是为每次迭代生成一个数据帧。
也许这就是您要找的。您可以使用 rename
函数重命名列:
for (row in 1:nrow(banks)){
bank = banks[row, "Company"]
reviews <- read.csv(paste(bank, "_cleaned.csv", sep = ""))
df_bank <- reviews %>%
group_by(Year_Quarter) %>%
summarise(general = mean(rat_general, na.rm = TRUE),
CV = mean(rat_CV, na.rm = TRUE),
WL = mean(rat_WL, na.rm = TRUE),
CO = mean(rat_CO, na.rm = TRUE),
CB = mean(rat_CB, na.rm = TRUE),
SM = mean(rat_SM, na.rm = TRUE)) %>%
rename_at(-1, ~paste0(.x, "_", bank)) %>%
as.data.frame()
df <- cbind(df, df_bank)
}
请注意,在开始循环之前,您必须初始化df
以获得正确的行数。
我想使用 for 循环为数据帧赋值,该循环遍历数据帧中的不同行。
我试图将我需要的变量名分配给一些值,这些变量名在每次迭代中都会发生变化,然后将这些值放在一个数据框中,但没有用
banks <- read.csv("banks_csv.csv", sep = ";", stringsAsFactors = FALSE)
banks<-data.frame(banks)
for (row in 1:nrow(banks)){
bank = banks[row, "Company"]
reviews <- read.csv(paste(bank, "_cleaned.csv", sep = ""))
reviews$X = NULL
reviews_summary <- reviews %>%
group_by(Year_Quarter) %>%
summarise(rat_gen = mean(rat_general, na.rm = TRUE),
CV = mean(rat_CV, na.rm = TRUE),
WL = mean(rat_WL, na.rm = TRUE),
CO = mean(rat_CO, na.rm = TRUE),
CB = mean(rat_CB, na.rm = TRUE),
SM = mean(rat_SM, na.rm = TRUE))
nam <- paste("general_", bank, sep = "")
assign(nam, reviews_summary$rat_gen)
nam <- paste("CV_", bank, sep = "")
assign(nam, reviews_summary$CV)
nam <- paste("WL_", bank, sep = "")
assign(nam, reviews_summary$WL)
nam <- paste("CO_", bank, sep = "")
as.list(nam, reviews_summary$CO)
nam <- paste("CB_", bank, sep = "")
as.list(nam, reviews_summary$CB)
nam <- paste("SM_", bank, sep = "")
as.list(nam, reviews_summary$SM)
df <- data.frame(paste("general_", bank, sep = ""), paste("CV_", bank, sep = ""), paste("WL_", bank, sep = ""), paste("CO_", bank, sep = ""), paste("CB_", bank, sep = ""), paste("SM_", bank, sep = ""))
}
编辑:我希望只有一个 data.frame,其中每次迭代添加 6 列,而不是为每次迭代生成一个数据帧。
也许这就是您要找的。您可以使用 rename
函数重命名列:
for (row in 1:nrow(banks)){
bank = banks[row, "Company"]
reviews <- read.csv(paste(bank, "_cleaned.csv", sep = ""))
df_bank <- reviews %>%
group_by(Year_Quarter) %>%
summarise(general = mean(rat_general, na.rm = TRUE),
CV = mean(rat_CV, na.rm = TRUE),
WL = mean(rat_WL, na.rm = TRUE),
CO = mean(rat_CO, na.rm = TRUE),
CB = mean(rat_CB, na.rm = TRUE),
SM = mean(rat_SM, na.rm = TRUE)) %>%
rename_at(-1, ~paste0(.x, "_", bank)) %>%
as.data.frame()
df <- cbind(df, df_bank)
}
请注意,在开始循环之前,您必须初始化df
以获得正确的行数。