R:是否可以使用paste0函数(或类似函数)将存储在对象中的数据传递给新对象?
抱歉,如果已经问过这个问题,我做了彻底的搜索,但找不到类似的例子。也许我没有用正确的词语来描述我想要实现的目标?我想这很容易做到。任何建议都将不胜感激 问题:R:是否可以使用paste0函数(或类似函数)将存储在对象中的数据传递给新对象?,r,object,variable-assignment,assign,assignment-operator,R,Object,Variable Assignment,Assign,Assignment Operator,抱歉,如果已经问过这个问题,我做了彻底的搜索,但找不到类似的例子。也许我没有用正确的词语来描述我想要实现的目标?我想这很容易做到。任何建议都将不胜感激 问题: set.seed(123) # Create fake data for each day where data was collected (in reality these are maps) ## Day 1 through... day1.thresh <- rnorm(5) day1.thresh ## ...da
set.seed(123)
# Create fake data for each day where data was collected (in reality these are maps)
## Day 1 through...
day1.thresh <- rnorm(5)
day1.thresh
## ...day XXX
dayXXX.thresh <- rnorm(5)
# Manually specify the day of interest
day <- 'day1'
# THESE DO NOT WORK:
# Assign a new name to a data object using the paste0
## ...using the normal assignment operator
newObject1 <- paste0(day, ".thresh")
newObject1
## ...using the <<- operator
newObject2 <<- paste0(day, ".thresh")
newObject2
## ...using assign()
assign("newObject3", paste0(day, ".thresh"), inherits = T)
newObject3
day1.thresh
>[1] -0.56047565 -0.23017749 1.55870831 0.07050839 0.12928774
newObject1
>[1] "day1.thresh"
newObject2
>[1] "day1.thresh"
newObject3
>[1] "day1.thresh"
我想知道是否有一种方法可以使用paste0函数或类似的方法将存储在对象中的数据传递给新对象,以便于通过使用包含字符串的第三个对象替换原始对象名称的一部分来将数据从存储对象传递给新对象。我想可能有一个赋值操作符可以做到这一点(您正在寻找
get
:
assign("newObject3", get(newObject1))
newObject3
#[1] -0.5605 -0.2302 1.5587 0.0705 0.1293
这很容易@Ronak,这就是我需要的!