lapply

Writing multiple data frames into .csv files using R

柔情痞子 提交于 2019-11-26 11:19:33
问题 I have used lapply to apply a function to a number of data frames: data.cleaned <- lapply(data.list, shooter_cleaning) And then labeled each of the resulting data frames in the list according to their subject number (e.g., 100): names(data.cleaned) <- subject.names What I want to do is to save each new data frame as an individual .csv file based on its subject number. For example, for subject 100 I\'d like the .csv file to be labeled as \"100.csv\" Normally to do this (for a single data frame

Returning anonymous functions from lapply - what is going wrong?

我的未来我决定 提交于 2019-11-26 08:25:25
问题 When trying to create a list of similar functions using lapply , I find that all the functions in the list are identical and equal to what the final element should be. Consider the following: pow <- function(x,y) x^y pl <- lapply(1:3,function(y) function(x) pow(x,y)) pl [[1]] function (x) pow(x, y) <environment: 0x09ccd5f8> [[2]] function (x) pow(x, y) <environment: 0x09ccd6bc> [[3]] function (x) pow(x, y) <environment: 0x09ccd780> When you try to evaluate these functions you get identical

lapply vs for loop - Performance R

旧街凉风 提交于 2019-11-26 03:57:47
问题 It is often said that one should prefer lapply over for loops. There are some exception as for example Hadley Wickham points out in his Advance R book. (http://adv-r.had.co.nz/Functionals.html) (Modifying in place, Recursion etc). The following is one of this case. Just for sake of learning, I tried to rewrite a perceptron algorithm in a functional form in order to benchmark relative performance. source (https://rpubs.com/FaiHas/197581). Here is the code. # prepare input data(iris) irissubdf

Faster way to read fixed-width files

大城市里の小女人 提交于 2019-11-26 03:55:44
问题 I work with a lot of fixed width files (i.e., no separating character) that I need to read into R. So, there is usually a definition of the column width to parse the string into variables. I can use read.fwf to read in the data without a problem. However, for large files, this can take a long time. For a recent dataset, this took 800 seconds to read in a dataset with ~500,000 rows and 143 variables. seer9 <- read.fwf(\"~/data/rawdata.txt\", widths = cols, header = FALSE, buffersize = 250000,

Read multiple CSV files into separate data frames

限于喜欢 提交于 2019-11-26 01:52:02
问题 Suppose we have files file1.csv , file2.csv , ... , and file100.csv in directory C:\\R\\Data and we want to read them all into separate data frames (e.g. file1 , file2 , ... , and file100 ). The reason for this is that, despite having similar names they have different file structures, so it is not that useful to have them in a list. I could use lapply but that returns a single list containing 100 data frames. Instead I want these data frames in the Global Environment. How do I read multiple

How do you read in multiple .txt files into R? [duplicate]

纵饮孤独 提交于 2019-11-26 01:39:51
问题 This question already has an answer here: How to import multiple .csv files at once? 10 answers I\'m using R to visualize some data all of which is in .txt format. There are a few hundred files in a directory and I want to load it all into one table, in one shot. Any help? EDIT: Listing the files is not a problem. But I am having trouble going from list to content. I\'ve tried some of the code from here, but I get a bug with this part: all.the.data <- lapply( all.the.files, txt , header=TRUE)

Access lapply index names inside FUN

梦想的初衷 提交于 2019-11-26 01:29:28
问题 Is there a way to get the list index name in my lapply() function? n = names(mylist) lapply(mylist, function(list.elem) { cat(\"What is the name of this list element?\\n\" }) I asked before if it\'s possible to preserve the index names in the lapply() returned list, but I still don\'t know if there is an easy way to fetch each element name inside the custom function. I would like to avoid to call lapply on the names themselves, I\'d rather get the name in the function parameters. 回答1:

Grouping functions (tapply, by, aggregate) and the *apply family

女生的网名这么多〃 提交于 2019-11-25 22:12:20
问题 Whenever I want to do something \"map\"py in R, I usually try to use a function in the apply family. However, I\'ve never quite understood the differences between them -- how { sapply , lapply , etc.} apply the function to the input/grouped input, what the output will look like, or even what the input can be -- so I often just go through them all until I get what I want. Can someone explain how to use which one when? My current (probably incorrect/incomplete) understanding is... sapply(vec, f