我有一个对象名称
Test12<-c(8,7,4,3,5,67,3,9,6,6,56,742,23,12,45,56)
我想从对象名称或Test12中提取最后两个数字,以分别获取数字1和2,并将它们放入两个对象中。
a<- 1
b<- 2
我尝试过
x <- name(Test12)
substrRight <- function(x, n){
substr(x, nchar(x)-n+1, nchar(x))
}
substrRight(x, 2)
[1] "8" "7" "4" "3" "5" "67" "3" "9" "6" "6" "56" "42" "23" "12" "45" "56"
但是,我得到每个对象元素的最后2个图标。
任何帮助都欢迎
nm <- bquote(Test12)
或
nm <- quote(Test12)
或
nm <- "Test12"
或
nm <- deparse(substitute(Test12))
根据@ Al.Sal的答案
将起作用,然后:
a <- substr(nm, nchar(nm)-1, nchar(nm)-1)
b <- substr(nm, nchar(nm), nchar(nm))
> a
[1] "1"
> b
[1] "2"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句