r - updating column values when table name is a variable -


first question here, , new r well.

i have loop creating data frames according list of studytables. can read csvs fine, field "subject" , add variable "study" before in field. trouble 2nd "assign" line, can't r assign new value "subject".

thanks help.

study <- 'study10' studytables <- list('ae', 'subject') studypath <- 'c:/mypath/'  for(table in studytables) {   destinframe <- paste(table,study, sep='')   file <- paste(studypath, table, '.csv', sep='' )   assign(destinframe, read.csv(file)) # create dataframes    assign(destinframe['subject'], rep('testing', nrow(get(destinframe)))) } 

using assign isn't great idea. , can see doesn't work when try add columns data.frame. it's better add columns before assign. replace

assign(destinframe, read.csv(file)) assign(destinframe['subject'], rep('testing', nrow(get(destinframe)))) 

with

dd <- read.csv(file) dd$subject <- paste(study, dd$subject) assign(destinframe, dd) 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -