parsing - R: How do you input a new column into a table with the eval(parse()) as table name? -
parsing - R: How do you input a new column into a table with the eval(parse()) as table name? -
i'm new r language may have missed out something... i'm trying run in loop.
assuming i<-1, , parti1 table.(dataframe)
partin<-paste("parti", i, sep = "") eval(parse(text = partin))["time"] <- "1"
however, gives error of
error in file(filename, "r") : cannot open connection in addition: warning message: in file(filename, "r") : cannot open file 'parti1': no such file or directory
but gives no error when this
eval(parse(text = partin))["time"]
or this.
parti1["time"]<-"1"
or this.
parti1<-eval(parse(text = paste("time", i, sep = "")))
i'm not sure if i'm doing wrong or if there's i'm missing. should not utilize eval(parse(mystring))
? if so, should utilize instead?
update:
input: old table:
name gender hobbies name f singing
output: new table:
name gender hobbies time name f singing 10:15
if understand question:
created 3 parti
datasets
parti1 <- as.data.frame(matrix(5:25, ncol=3, dimnames=list(null, c("col1", "col2", "time")))) parti2 <- as.data.frame(matrix(1:15, ncol=3, dimnames=list(null, c("col1", "col2", "time")))) parti3 <- as.data.frame(matrix(1:15, ncol=3, dimnames=list(null, c("col1", "col2", "time"))))
used ls
, mget
values of datasets
for example,
ls(pattern="^parti") #gives created object names starts `parti` in working environment. #[1] "parti1" "parti2" "parti3" lst1 <- mget(ls(pattern="^parti")) #mget homecoming list of values of each `parti` object
if need alter column time
1 each datasets, there couple of ways. methods below created columns within list environment. alter original datasets, have assign
or list2env
map(function(x, y) {x[,"time"] <- y; x}, lst1, 1)
or
lapply(lst1, function(x) {x$time <-1; x}) #i changing existing `time` column 1 using assignment operator `<-`.
if want create new column, do:
lapply(lst1, function(x) {x$time1 <-1; x})
update if need change/create columns in original dataset,
partin <- paste0("parti", 1) assign(partin, `[[<-`(get(partin),'time', value=1))
the above method take string
i.e. partin
first argument, , uses replacement function [[<-
. values of partin
utilize get
. next argument column modify or create ie. time
, assign value of 1
column.
parti1 # col1 col2 time #1 5 12 1 #2 6 13 1 #3 7 14 1 #4 8 15 1 #5 9 16 1 #6 10 17 1 #7 11 18 1
for multiple datasets, change/create existing column/new column, utilize for
loop assign
or utilize list2env
partin1 <- paste0("parti", 1:3) for(i in seq_along(partin1)){ assign(partin1[i], `[[<-`(get(partin1[i]), 'time1', value=5)) #creating new column } parti2 # col1 col2 time time1 #1 1 6 11 5 #2 2 7 12 5 #3 3 8 13 5 #4 4 9 14 5 #5 5 10 15 5
this done using list2env
list2env(lapply(mget(partin1), function(x) {x$time2 <- 10 ;x}), envir=.globalenv)
but, suggest doing analysis in list rather creating objects.
update2method using eval(parse
partin <- paste0("parti", 1) toassign <- paste0(partin, "[,'time3']") str1 <- paste0(toassign, "<-", 15) eval(parse(text=str1)) parti1 # col1 col2 time time3 #1 5 12 19 15 #2 6 13 20 15 #3 7 14 21 15 #4 8 15 22 15 #5 9 16 23 15 #6 10 17 24 15 #7 11 18 25 15
using illustration dataset provided
toassign <- paste0("oldtbl", "[, 'time']") str1 <- paste0(toassign, "<-", "'10:15'") eval(parse(text=str1)) oldtbl # name gender hobbies time #1 name false singing 10:15
data oldtbl <- structure(list(name = "name", gender = false, hobbies = "singing", time = "10:15"), row.names = c(na, -1l), .names = c("name", "gender", "hobbies", "time"), class = "data.frame")
r parsing variable-names
Comments
Post a Comment