Executing function on objects of name 'i' within for-loop in R -


i still pretty new r , new for-loops , functions, searched quite bit on stackoverflow , couldn't find answer question. here go.

i'm trying create script (1) read in multiple .csv files , (2) apply function strip twitter handles urls in , other things these files. have developed script these 2 tasks separately, know of code works, goes wrong when try combine them. prepare doing using following code:

# specify directory files , replace 'file' first, unique part of # files import mypath <- "~/users/you/data/" mypattern <- "file+.*csv"  # list of files file_list <- list.files(path = mypath,                         pattern = mypattern)  # list of names given data frames data_names <- str_match(file_list, "(.*?)\\.")[,2]  # define function preparing datasets handlestripper <- function(data){   data$handle <- str_match(data$url, "com/(.*?)/status")[,2]   data$rank <- c(1:500)   names(data) <- c("dategmt", "url", "tweet", "twitterid", "rank")   data <- data[,c(4, 1:3, 5)] } 

that works fine. problem comes when try execute function handlestripper() within for-loop.

# read in data for(i in data_names){   filepath <- file.path(mypath, paste(i, ".csv", sep = ""))   assign(i, read.delim(filepath, colclasses = "character", sep = ","))   <- handlestripper(i) } 

when execute code, following error: error in data$url : $ operator invalid atomic vectors. know means function being applied string called within vector data_names, don't know how tell r that, in last line of for-loop, want function applied objects of name i created using assign command, rather i itself.

inside loop, can change this:

assign(i, read.delim(filepath, colclasses = "character", sep = ",")) <- handlestripper(i) 

to

tmp <- read.delim(filepath, colclasses = "character", sep = ",") assign(i, handlestripper(tmp)) 

i think should make few get , assign calls can, there's nothing wrong indexing loop names doing. time, anyway.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -