It is hard to know whether the strings in which you are trying to manipulate and then collate into columns in a data.frame
follow a pattern. But for the example you posted, I suggest creating a list with the strings (strings
):
strings <- list(string, text)
Then use lapply()
which will in turn create a list for each element in strings
.
res <-lapply(strings, function(x){
grep(x=trimws(unlist(strsplit(x, "\s\s"))), pattern="[[:alpha:]]", value=TRUE)
})
In the code above, strsplit()
splits the string whenever two spaces are found (\s\s
). But the resulting split is a list with the strings as inner elements. Therefore you need to use unlist()
so you can use it with grep()
. grep()
will select only those strings with an alphanumeric character --which is what you want.
You can then use do.call(cbind, list)
to bind the elements in the resulting lapply()
list into columns. The dimension must match for this work.
do.call(cbind, res)
Result:
> do.call(cbind, res)
[,1] [,2]
[1,] "A" "I love"
[2,] "B" "chocolate"
[3,] "C" "pudding"
You can wrap it up into a as.data.frame()
for instance to get the desired result:
> as.data.frame(do.call(cbind, res), stringsAsFactors = FALSE)
V1 V2
1 A I love
2 B chocolate
3 C pudding
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…