簡體   English   中英

根據 R 中的字幕分組添加數據框列

[英]add a dataframe column based on grouping by subtitles in R

試圖將字幕移動到以下數據幀的第三列:

library(tidyverse)
library(data.table)
library(rvest)

url <- "https://resources.companieshouse.gov.uk/sic/"
list <-  read_html(url) %>% 
  html_nodes("table") %>% 
  html_table()

# convert nested list to df
df <- data.frame(Reduce(rbind, list))

我已經完成了 21 個組和他們的名字:

groups <- cumsum(df[,1] %like% "Section")
group_names <- data.frame(df[df[,1] %like% "Section", 2])

但找不到適用於df %>% group_by()的 dplyr 解決方案

也許您正在嘗試這樣做:

library(dplyr)
df %>%
  mutate(group_name = replace(Code, !grepl('Section', Code), NA)) %>%
  tidyr::fill(group_name)

#       Code                                                      Description group_name
#1 Section A                                Agriculture, Forestry and Fishing  Section A
#2     01110 Growing of cereals (except rice), leguminous crops and oil seeds  Section A
#3     01120                                                  Growing of rice  Section A
#4     01130               Growing of vegetables and melons, roots and tubers  Section A
#5     01140                                            Growing of sugar cane  Section A
#6     01150                                               Growing of tobacco  Section A
#...

我將所有包含"Section"Code移動到不同的列中並fill缺失值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM