यहां विभिन्न आधार स्ट्रिंग कार्यों के कुछ का उपयोग कर एक संभावना है।
## get the lengths of the output for each first name
len <- lengths(gregexpr("/", sub(" .*", "", a), fixed = TRUE)) + 1L
## extract all the first names
## using the fact that they all end at the first space character
fn <- scan(text = a, sep = "/", what = "", comment.char = " ")
## paste them together
paste0(fn, rep(regmatches(a, regexpr(" .*", a)), len))
# [1] "tim meyer XY900 123kncjd" "tom meyer XY900 123kncjd"
# [3] "sepp moser VK123 456xyz" "max moser VK123 456xyz"
# [5] "peter moser VK123 456xyz"
अलावा: यहाँ एक दूसरे संभावना, थोड़ा कम कोड का उपयोग कर रहा है। थोड़ा तेज भी हो सकता है।
s <- strsplit(a, "\\/|(.*)")
paste0(unlist(s), rep(regmatches(a, regexpr(" .*", a)), lengths(s)))
# [1] "tim meyer XY900 123kncjd" "tom meyer XY900 123kncjd"
# [3] "sepp moser VK123 456xyz" "max moser VK123 456xyz"
# [5] "peter moser VK123 456xyz"
स्रोत
2016-02-05 15:10:14
बिल्कुल सही! ..no लूप और बेस फ़ंक्शन बिल्कुल ठीक है जो मैं बाद में था;) – Kay
अच्छा समाधान रिचर्ड –
वह दूसरा समाधान मनी मैन 1 प्लस है –