在R中使用分隔符将文本拆分为两列

g6baxovj  于 2023-05-26  发布在  其他
关注(0)|答案(3)|浏览(166)

我尝试使用分隔符"|"拆分列,但不幸的是,Stack Overflow中推荐使用separator()的建议在我的情况下不起作用。
有人能帮我一下吗。
我使用的 Dataframe 如下所示:

structure(list(UniqueID = c("12M-MA | X1", "12M-MA | X2", 
"12M-MA | X3", "12M-MA | X4", "12M-MA | X5"
), Cost = c(0.2, 0.3, 0.2, 0.2, 412.86), Actuals = c(0, 
0, 0, 0, 32), Forecast = c(0, 0, 0, 0, 21), Value_Actuals = c(0, 
0, 0, 0, 28341), Value_Forecast = c(0, 0, 0, 0, 652431
), `Forecast Accuracy` = c(0, 0, 0, 0, 8.51), `Max (Act vs Cons)` = c(0, 
0, 0, 0, 652431)), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))

我使用的代码和正在工作的代码是这样的:

library(readxl)
library(dplyr)
library(writexl)
library(stringr)
Df<- read_excel("C:/X/X/X/X/YXZ-2023.xlsx"skip = 1)

Df <- Df %>% 
  separate(UniqueID,c("ABC","XYZ"),sep = "|")

我们期望得到的是保持列不变,并得到另外两列,一列的名称为“ABC”,另一列的名称为“XYZ”。

12-MA应放入“ABC”列,其他值如X1、X2、X3、X4、X5应放入“XYZ”列。

谁能帮帮我,让我知道我做错了什么。这是堆栈溢出和无处不在的建议,但对我不起作用。

8mmmxcuj

8mmmxcuj1#

我们必须用以下方式转义|\\
R中的\\转义特殊字符。参见here
下面是一个特殊字符列表。
为了保留原始列,我们使用remove = FALSE参数。
最后,我们使用acrosstrimws来删除空格。

library(dplyr)
library(tidyr)

df %>%    
  separate(UniqueID,c("ABC","XYZ"),sep = "\\|", remove = FALSE) %>% 
  mutate(across(c(ABC, XYZ), trimws))

 UniqueID    ABC    XYZ    Cost Actuals Forecast Value_Actuals Value_Forecast `Forecast Accuracy` `Max (Act vs Cons)`
  <chr>       <chr>  <chr> <dbl>   <dbl>    <dbl>         <dbl>          <dbl>               <dbl>               <dbl>
1 12M-MA | X1 12M-MA X1      0.2       0        0             0              0                0                      0
2 12M-MA | X2 12M-MA X2      0.3       0        0             0              0                0                      0
3 12M-MA | X3 12M-MA X3      0.2       0        0             0              0                0                      0
4 12M-MA | X4 12M-MA X4      0.2       0        0             0              0                0                      0
5 12M-MA | X5 12M-MA X5    413.       32       21         28341         652431                8.51              652431
nhn9ugyo

nhn9ugyo2#

另一个解决方案是extract

library(tidyr)
df %>%
  extract(UniqueID,
          c("ABC", "XYZ"),
          "(.*)\\s\\|\\s(.*)")
# A tibble: 5 × 9
  ABC    XYZ    Cost Actuals Forecast Value_Actuals Value_Forecast `Forecast Accuracy` `Max (Act vs Cons)`
  <chr>  <chr> <dbl>   <dbl>    <dbl>         <dbl>          <dbl>               <dbl>               <dbl>
1 12M-MA X1      0.2       0        0             0              0                0                      0
2 12M-MA X2      0.3       0        0             0              0                0                      0
3 12M-MA X3      0.2       0        0             0              0                0                      0
4 12M-MA X4      0.2       0        0             0              0                0                      0
5 12M-MA X5    413.       32       21         28341         652431                8.51              652431
zhte4eai

zhte4eai3#

你有没有试过新的separate_wider_delimcols_remove = F

require(tidyverse)
# require(tidyr) # if you do not want to import `tidyverse`

Df %>% separate_wider_delim(UniqueID, delim = "|", names = c("ABC", "XYZ"), cols_remove = F)
Df %>% separate_wider_delim(UniqueID, delim = " | ", names = c("ABC", "XYZ"), cols_remove = F) # if you do not want " " in the new columns

# A tibble: 5 × 10
  ABC    XYZ   UniqueID     Cost Actuals Forecast Value_Actuals Value_Forecast `Forecast Accuracy` `Max (Act vs Cons)`
  <chr>  <chr> <chr>       <dbl>   <dbl>    <dbl>         <dbl>          <dbl>               <dbl>               <dbl>
1 12M-MA X1    12M-MA | X1   0.2       0        0             0              0                0                      0
2 12M-MA X2    12M-MA | X2   0.3       0        0             0              0                0                      0
3 12M-MA X3    12M-MA | X3   0.2       0        0             0              0                0                      0
4 12M-MA X4    12M-MA | X4   0.2       0        0             0              0                0                      0
5 12M-MA X5    12M-MA | X5 413.       32       21         28341         652431                8.51              652431

相关问题