拆分具有“ |”的行使用split()fn不拆分

时间:2018-10-18 05:36:17

标签: r tidyr

我的数据看起来像

> company
                          name                             category_list
11                     1-4 All              Entertainment|Games|Software
12            1.618 Technology        Networking|Real Estate|Web Hosting
13               1-800-DENTIST                       Health and Wellness
14               1-800-DOCTORS                       Health and Wellness
15 1-800-PublicRelations, Inc. Internet Marketing|Media|Public Relations

我将不得不根据值拆分category_list列。当值用管道分隔时,应将行拆分。

我使用单独的函数尝试了相同的操作,但该列未填充任何值

c1 <- company %>% separate(category_list,into=c("primary_Sector"), sep="|")

实际输出:

                      name primary_Sector
11                     1-4 All               
12            1.618 Technology               
13               1-800-DENTIST               
14               1-800-DOCTORS               
15 1-800-PublicRelations, Inc.

预期产量

    name      category_list
11 1-4 All    Entertainment
12 1-4 All    Games
13 1-4 All    Software

有人可以告诉我怎么了吗?

1 个答案:

答案 0 :(得分:1)

tidyr::separate()进行列分隔,tidyr::separate_rows()进行行分隔:

library(tidyr)

read.table(
  text="name;category_list
1-4 All;Entertainment|Games|Software
1.618 Technology;Networking|Real Estate|Web Hosting
1-800-DENTIST;Health and Wellness
1-800-DOCTORS;Health and Wellness
1-800-PublicRelations, Inc.;Internet Marketing|Media|Public Relations", 
  sep=";", header = TRUE, stringsAsFactors = FALSE
) %>% 
  separate_rows(category_list, sep = "\\|")
##                           name       category_list
## 1                      1-4 All       Entertainment
## 2                      1-4 All               Games
## 3                      1-4 All            Software
## 4             1.618 Technology          Networking
## 5             1.618 Technology         Real Estate
## 6             1.618 Technology         Web Hosting
## 7                1-800-DENTIST Health and Wellness
## 8                1-800-DOCTORS Health and Wellness
## 9  1-800-PublicRelations, Inc.  Internet Marketing
## 10 1-800-PublicRelations, Inc.               Media
## 11 1-800-PublicRelations, Inc.    Public Relations