如何根据 R 中的其他列值将值放入列中
How to put values inside a column based on other column values in R
我正在使用 R 为我作为记者的工作抓取和清理数据。我可以获得 HTML 的 table,然后将其作为数据框读取并重命名列的名称。现在我正在尝试创建一个新列,该列获取一个考虑到其他列值的值。
这个新列应该得到 "Avante"、"DEM"、"MDB"、"Patriota"、"PCdoB" 等的值。这是每个代表的聚会。比如Avante,有3个副手,分别是"Adalberto Cavalcanti"、"Cabo Sabino"和"Silvio Costa"。人大代表的名字总是在党名整排的下面。
url <- "http://www.camara.leg.br/internet/votacao/mostraVotacao.asp?ideVotacao=8559&numLegislatura=55&codCasa=1&numSessaoLegislativa=4&indTipoSessaoLegislativa=O&numSessao=225&indTipoSessao=E&tipo=partido"
library(xml2)
library(rvest)
file <- read_html(url)
tables <- html_nodes(file, "table")
table1 <- html_table(tables[3], fill = TRUE, header = T)
head(table1)
table1_df <- as.data.frame(table1)
colnames(table1_df) <- c("deputado", "uf", "voto")
这是我现在拥有的:
enter image description here
这就是我想要的:
enter image description here
这是一个只使用 base R 的解决方案:
url <- "http://www.camara.leg.br/internet/votacao/mostraVotacao.asp?ideVotacao=8559&numLegislatura=55&codCasa=1&numSessaoLegislativa=4&indTipoSessaoLegislativa=O&numSessao=225&indTipoSessao=E&tipo=partido"
library(xml2)
library(rvest)
file <- read_html(url)
tables <- html_nodes(file, "table")
table1 <- html_table(tables[3], fill = TRUE, header = T)
head(table1)
table1_df <- as.data.frame(table1)
colnames(table1_df) <- c("deputado", "uf", "voto")
# create the new column for later
table1_df$new_column <- NA
# identify rows with the Total PARTY: NUM rows
idx <- grep("Total.*: \d+", table1_df$deputado)
# Loop over these and assign the values
for (i in seq_along(idx)){
# Extract the number of deputados
n <- as.numeric(sub("^.*: ", "", table1_df$deputado[idx[i]]))
# Extract the party
partido <- sub("Total ", "", table1_df$deputado[idx[i]])
partido <- sub(": .*", "", partido)
# Assign the values
table1_df$new_column[(idx[i] - n):(idx[i] - 1)] <- partido
}
# Remove the unnecessary lines
table1_df <- table1_df[-grep("Total .*:.*", table1_df$deputado), ]
table1_df <- table1_df[-which(table1_df$deputado == table1_df$uf), ]
这是另一个使用 zoo
和 dplyr
的选项。
1) 获取当事人姓名
parties <- sub(pattern = "Total\s(.+):\s\d+",
replacement = "\1",
x = table1_df$deputado[grepl("Total", x = table1_df$deputado)])
2) 添加 parties
作为新列,并在前面进行最后的观察,因为 parties[match(table1_df$deputado, parties)]
.
中有许多 NA
table1_df$new_col <- zoo::na.locf(parties[match(table1_df$deputado, parties)])
3) 删除不需要的行。
library(dplyr)
table1_df <- table1_df %>%
group_by(new_col) %>%
slice(2:(n()-1))
table1_df
# A tibble: 324 x 4
# Groups: new_col [24]
# deputado uf voto new_col
# <chr> <chr> <chr> <chr>
# 1 Adalberto Cavalcanti PE Não Avante
# 2 Cabo Sabino CE Abstenção Avante
# 3 Silvio Costa PE Sim Avante
# 4 Alan Rick AC Sim DEM
# 5 Alberto Fraga DF Não DEM
# 6 Alexandre Leite SP Sim DEM
# 7 Arthur Oliveira Maia BA Sim DEM
# 8 Carlos Melles MG Sim DEM
# 9 Efraim Filho PB Não DEM
#10 Eli Corrêa Filho SP Sim DEM
# ... with 314 more rows
我正在使用 R 为我作为记者的工作抓取和清理数据。我可以获得 HTML 的 table,然后将其作为数据框读取并重命名列的名称。现在我正在尝试创建一个新列,该列获取一个考虑到其他列值的值。
这个新列应该得到 "Avante"、"DEM"、"MDB"、"Patriota"、"PCdoB" 等的值。这是每个代表的聚会。比如Avante,有3个副手,分别是"Adalberto Cavalcanti"、"Cabo Sabino"和"Silvio Costa"。人大代表的名字总是在党名整排的下面。
url <- "http://www.camara.leg.br/internet/votacao/mostraVotacao.asp?ideVotacao=8559&numLegislatura=55&codCasa=1&numSessaoLegislativa=4&indTipoSessaoLegislativa=O&numSessao=225&indTipoSessao=E&tipo=partido"
library(xml2)
library(rvest)
file <- read_html(url)
tables <- html_nodes(file, "table")
table1 <- html_table(tables[3], fill = TRUE, header = T)
head(table1)
table1_df <- as.data.frame(table1)
colnames(table1_df) <- c("deputado", "uf", "voto")
这是我现在拥有的: enter image description here
这就是我想要的: enter image description here
这是一个只使用 base R 的解决方案:
url <- "http://www.camara.leg.br/internet/votacao/mostraVotacao.asp?ideVotacao=8559&numLegislatura=55&codCasa=1&numSessaoLegislativa=4&indTipoSessaoLegislativa=O&numSessao=225&indTipoSessao=E&tipo=partido"
library(xml2)
library(rvest)
file <- read_html(url)
tables <- html_nodes(file, "table")
table1 <- html_table(tables[3], fill = TRUE, header = T)
head(table1)
table1_df <- as.data.frame(table1)
colnames(table1_df) <- c("deputado", "uf", "voto")
# create the new column for later
table1_df$new_column <- NA
# identify rows with the Total PARTY: NUM rows
idx <- grep("Total.*: \d+", table1_df$deputado)
# Loop over these and assign the values
for (i in seq_along(idx)){
# Extract the number of deputados
n <- as.numeric(sub("^.*: ", "", table1_df$deputado[idx[i]]))
# Extract the party
partido <- sub("Total ", "", table1_df$deputado[idx[i]])
partido <- sub(": .*", "", partido)
# Assign the values
table1_df$new_column[(idx[i] - n):(idx[i] - 1)] <- partido
}
# Remove the unnecessary lines
table1_df <- table1_df[-grep("Total .*:.*", table1_df$deputado), ]
table1_df <- table1_df[-which(table1_df$deputado == table1_df$uf), ]
这是另一个使用 zoo
和 dplyr
的选项。
1) 获取当事人姓名
parties <- sub(pattern = "Total\s(.+):\s\d+",
replacement = "\1",
x = table1_df$deputado[grepl("Total", x = table1_df$deputado)])
2) 添加 parties
作为新列,并在前面进行最后的观察,因为 parties[match(table1_df$deputado, parties)]
.
NA
table1_df$new_col <- zoo::na.locf(parties[match(table1_df$deputado, parties)])
3) 删除不需要的行。
library(dplyr)
table1_df <- table1_df %>%
group_by(new_col) %>%
slice(2:(n()-1))
table1_df
# A tibble: 324 x 4
# Groups: new_col [24]
# deputado uf voto new_col
# <chr> <chr> <chr> <chr>
# 1 Adalberto Cavalcanti PE Não Avante
# 2 Cabo Sabino CE Abstenção Avante
# 3 Silvio Costa PE Sim Avante
# 4 Alan Rick AC Sim DEM
# 5 Alberto Fraga DF Não DEM
# 6 Alexandre Leite SP Sim DEM
# 7 Arthur Oliveira Maia BA Sim DEM
# 8 Carlos Melles MG Sim DEM
# 9 Efraim Filho PB Não DEM
#10 Eli Corrêa Filho SP Sim DEM
# ... with 314 more rows