Ich Folgern, dass, wenn Sie sagen, "nicht funktioniert", ist es, weil das Prozentzeichen entfernt wird:
separate(data_frame(a=c("45 (10, 89)","34")), a, c('x','y','z'), extra="drop")
# Warning: Too few values at 1 locations: 2
# # A tibble: 2 × 3
# x y z
# * <chr> <chr> <chr>
# 1 45 10 89
# 2 34 <NA> <NA>
separate(data_frame(a=c("45% (10, 89)","34%")), a, c('x','y','z'), extra="drop")
# Warning: Too few values at 1 locations: 2
# # A tibble: 2 × 3
# x y z
# * <chr> <chr> <chr>
# 1 45 10 89
# 2 34 <NA>
Von ?separate
:
separate(data, col, into, sep = "[^[:alnum:]]+", remove = TRUE,
convert = FALSE, extra = "warn", fill = "warn", ...)
...
Da Sie Überschreiben Sie nicht den Standardwert sep
, es findet alles, was kein Buchstabe oder eine Zahl ist. FYI, [^[:alnum:]]+
ist analog zu [^A-Za-z0-9]+
, was "1 oder mehr Zeichen entspricht, die nicht in den Zeichenbereichen von A-Z, a-z oder 0-9" sind.
Geben Sie einfach eine detailliertere sep
, und Sie werden bekommen, was Sie wollen.
separate(data_frame(a=c("45% (10, 89)","34%")), a, c('x','y','z'), sep="[^[:alnum:]%]+", extra="drop")
# Warning: Too few values at 1 locations: 2
# # A tibble: 2 × 3
# x y z
# * <chr> <chr> <chr>
# 1 45% 10 89
# 2 34% <NA> <NA>
bearbeiten: Ihre letzte Beispieldaten:
separate(data_frame(a=c("45% (10/89)","34%","","67%","78% (89/98)")), a, c('x','y','z'), sep="[^[:alnum:]%]+", extra="drop")
# Warning: Too few values at 3 locations: 2, 3, 4
# # A tibble: 5 × 3
# x y z
# * <chr> <chr> <chr>
# 1 45% 10 89
# 2 34% <NA> <NA>
# 3 <NA> <NA>
# 4 67% <NA> <NA>
# 5 78% 89 98
Es funktioniert für mich. – Psidom
Was bedeutet "nicht funktionieren" genau für dich? – MrFlick
Anstatt 45 10 89, bekomme ich 7 7 6, ich denke, es ist ein Faktor und die Zahlen entsprechen diesen Faktoren. – Mee