I have a number of dataframes as R objects named with a prefix and a number. Each dataframe has same column names and numbers as row names.
I am trying to add a column based on the object iteration. I am trying the following:
x <- 1
repeat{
get(paste0('prefix', x))$iteration <- x-1
x = x + 1
}
However, I get the following error:
target of assignment expands to non-language object
Could you explain why this does not work and suggest a solution?
Thanks.
Get the values of objects that starts with 'prefix' followed by any digits into a list
('lst1'), then use Map
to create a new column by subtracting 1 from the numeric part of the object name ('v1'), use list2env
to reflect the changes in the original objects in global env
lst1 <- mget(ls(pattern = '^prefix\\d+$'))
v1 <- as.numeric(sub("prefix", "", names(lst1))) -1
lst1 <- Map(cbind, lst1, iteration = v1)
list2env(lst1, .GlobalEnv)
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.