I have a very similar question to this one , but I have a more complicated situation.
Here is my sample code:
test = data.frame(x = c(1:4),
y = c("/abc/werts/h1-1234", "/abc/fghye/seths/h2-234",
"/abc/gvawrttd/hyeadar/h3-9868", "/abc/qqras/x1-7653"))
test$y = as.character(test$y)
And I want an output like this:
1 h1-1234
2 h2-234
3 h3-9868
4 x1-7653
I tried:
test$y = tail(unlist(strsplit(test$y, "/")), 1)
However, the result of above codes returned:
1 h1-1234
2 h1-1234
3 h1-1234
4 h1-1234
So my question is, how should modified my code so that I can get my desired output?
Thanks in advance!
Here is the line you are looking for:
test$y = sapply(strsplit(test$y, "/"), tail, 1)
It applies tail
to each element in the list returned by strsplit
.
Here is an option using sub
to match zero or more characters ( .*
) followed by /
( \\\\/
) followed by zero or more characters that are not a /
captured as a group ( ([^/]*)
) until the end ( $
) of the string, and replace with the backreference ( \\\\1
) of the capture group
test$y <- sub(".*\\/([^/]*)$", "\\1", test$y)
test$y
#[1] "h1-1234" "h2-234" "h3-9868" "x1-7653"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.