Example:
#Create an object
OldName <- c(1:10)
#Create new object for each loop but assign them into new object names
for (i in 1:10) {
assign(paste0("NewName", i), OldName[i])
}
#Check
> NewName1
[1] 1
> NewName2
[1] 2
> NewName3
[1] 3
> sapply(ls(pattern = "^NewName[0-9]+"), get)
NewName1 NewName10 NewName2 NewName3 NewName4 NewName5 NewName6 NewName7
1 10 2 3 4 5 6 7
NewName8 NewName9
8 9
No comments:
Post a Comment