[英]How to concatenete strings after str_split
Given this data frame 鉴于此数据框架
column_1 column_2
A w,x
B z
C q,r,s
My desired output would be 我想要的输出是
"Aw", "Ax", "Bz", "Cq", "Cr", "Cs"
I've tried 我试过了
paste0(df$column_1, strsplit(df$column_2, ","))
But the output is 但输出是
"Ac(\"w\", \"x\")" "Bz" "Cc(\"q\", \"r\", \"s\")"
We can split column_2
on "," and paste them with column_1
using mapply
我们可以在“,”上拆分column_2
,然后使用column_1
将mapply
粘贴到mapply
unlist(mapply(paste0, df$column_1,strsplit(df$column_2, ",")))
#[1] "Aw" "Ax" "Bz" "Cq" "Cr" "Cs"
We can rep
licate the 'column_1' by the lengths
of list
output from strsplit
and then do the paste
我们可以rep
licate的“COLUMN_1”由lengths
的list
,从输出strsplit
然后做paste
lst1 <- strsplit(df$column_2, ",")
paste0(rep(df$column_1, lengths(lst1)), unlist(lst1))
#[1] "Aw" "Ax" "Bz" "Cq" "Cr" "Cs"
NOTE: The above is a vectorized approach as we are not looping through the list
注意:上面是一个矢量化方法,因为我们没有循环遍历list
Or use stack
to create a two column data.frame from list
and then paste
或者使用stack
从list
创建两列data.frame,然后paste
do.call(paste0, stack(setNames(lst1, df$column_1))[2:1])
#[1] "Aw" "Ax" "Bz" "Cq" "Cr" "Cs"
stack
ing to a two column data.frame approach may be a bit less efficient compared to the first approach 与第一种方法相比, stack
到两列data.frame方法可能效率稍低
Or with tidyverse
, split the 'column_2' to long format with separate_rows
, then unite
the two columns and pull
it to vector
或者与tidyverse
,分拆'COLUMN_2为长格式separate_rows
,然后unite
的两列pull
它vector
library(tidyverse)
df %>%
separate_rows(column_2) %>%
unite(newcol, column_1, column_2, sep="") %>%
pull(newcol)
#[1] "Aw" "Ax" "Bz" "Cq" "Cr" "Cs"
The issue in the OP's approach is based on the fact that the strsplit
output is a list
of vector
s. OP的方法中的问题是基于strsplit
输出是vector
list
的事实。 We need a function to loop over the list
( lapply/sapply/vapply
) or unlist
the list
into a vector
while replicating the
'column_1' (to make the length
during paste
ing) 我们需要一个函数循环遍历list
( lapply/sapply/vapply
)或将list
unlist
list
转换为vector
同时replicating the
'column_1'(在paste
过程中制作length
)
df <- structure(list(column_1 = c("A", "B", "C"), column_2 = c("w,x",
"z", "q,r,s")), class = "data.frame", row.names = c(NA, -3L))
This can also be achieved using below code. 这也可以使用下面的代码来实现。 Although not very idiomatic 虽然不是很惯用
df <- data.frame(column_1 = c("A", "B", "C"), column_2 = c("w,x", "z", "q,r,s"))
l_vals <- strsplit(as.character(df$column_2), split = ",", perl =TRUE)
l_append = list()
for(i in seq_along(l_vals)){
l_append <- c(l_append,paste0(df$column_1[i], l_vals[[i]]))
}
unlist(l_append)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.