我有以下的數據幀如何分割列,並保持第一分裂值代表一個
library(tidyverse)
#> + ggplot2 2.2.1.9000 Date: 2017-10-10
#> + tibble 1.3.4 R: 3.3.2
#> + tidyr 0.7.1 OS: macOS Sierra 10.12.6
#> + readr 1.1.1 GUI: X11
#> + purrr 0.2.3 Locale: en_US.UTF-8
#> + dplyr 0.7.3 TZ: Asia/Tokyo
#> + stringr 1.2.0
#> + forcats 0.2.0
#> ── Conflicts ────────────────────────────────────────────────────
#> * filter(), from dplyr, masks stats::filter()
#> * lag(), from dplyr, masks stats::lag()
DF <- data.frame(V1=c("Place1-Place2-Place2-Place4-Place2-Place3-Place5",
"Place7",
"Place6-Place6",
"Place1-Place2-Place3-Place4"),
V2=c(100,200,500,10)) %>%
as.tibble()
DF
#> # A tibble: 4 x 2
#> V1 V2
#> <fctr> <dbl>
#> 1 Place1-Place2-Place2-Place4-Place2-Place3-Place5 100
#> 2 Place7 200
#> 3 Place6-Place6 500
#> 4 Place1-Place2-Place3-Place4 10
我想要做的是分裂V1
「 - 」和挑首先使用第一個分割值作爲V1
的新值。導致:
V1 V2
Place1 100
Place7 200
Place6 500
Place1 10
我堅持這一點:
as.data.frame(str_split_fixed(DF$V1, "-",8))
V1 V2 V3 V4 V5 V6 V7 V8
1 Place1 Place2 Place2 Place4 Place2 Place3 Place5
2 Place7
3 Place6 Place6
4 Place1 Place2 Place3 Place4
@Jimbou這不行。 – scamander
嘗試'DF%>%mutate(index = 1:n())%>%separate_rows(V1,sep =「 - 」)%>%group_by(index)%>%slice(1)' – Jimbou