do.call

Selecting rows from a data frame from combinations of lists [duplicate]

北战南征 提交于 2019-12-02 09:30:51
This question already has an answer here: Removing one table from another in R [closed] 3 answers I have a dataframe, dat: dat<-data.frame(col1=rep(1:4,3), col2=rep(letters[24:26],4), col3=letters[1:12]) I want to filter dat on two different columns using ONLY the combinations given by the rows in the data frame filter : filter<-data.frame(col1=1:3,col2=NA) lists<-list(list("x","y"),list("y","z"),list("x","z")) filter$col2<-lists So for example, rows containing (1,x) and (1,y), would be selected, but not (1,z),(2,x), or (3,y). I know how I would do it using a for loop: #create a frame to drop

Non-standard evaluation in a user-defined function with lapply or with in R

梦想与她 提交于 2019-12-02 01:49:09
问题 I wrote a wrapper around ftable because I need to compute flat tables with frequency and percentage for many variables. As ftable method for class "formula" uses non-standard evaluation, the wrapper relies on do.call and match.call to allow the use of the subset argument of ftable (more details in my previous question). mytable <- function(...) { do.call(what = ftable, args = as.list(x = match.call()[-1])) # etc } However, I cannot use this wrapper with lapply nor with : # example 1: error

Non-standard evaluation in a user-defined function with lapply or with in R

梦想与她 提交于 2019-12-02 00:05:19
I wrote a wrapper around ftable because I need to compute flat tables with frequency and percentage for many variables. As ftable method for class "formula" uses non-standard evaluation, the wrapper relies on do.call and match.call to allow the use of the subset argument of ftable (more details in my previous question ). mytable <- function(...) { do.call(what = ftable, args = as.list(x = match.call()[-1])) # etc } However, I cannot use this wrapper with lapply nor with : # example 1: error with "lapply" lapply(X = warpbreaks[c("breaks", "wool", "tension")], FUN = mytable, row.vars = 1) Error

Loop linear regression and saving ALL coefficients

℡╲_俬逩灬. 提交于 2019-12-01 01:51:10
Based on the link below, I created a code to run regression on subsets of my data based on a variable. Loop linear regression and saving coefficients In this example I created a DUMMY (0 or 1) to create the subsets (in reality I have 3000 subsets) res <- do.call(rbind, lapply(split(mydata, mydata$DUMMY),function(x){ fit <- lm(y~x1 + x2, data=x) res <- data.frame(DUMMY=unique(x$DUMMY), coeff=coef(fit)) res })) This results in the following dataset DUMMY coeff 0.(Intercept) 0 22.8419956 0.x1 0 -11.5623064 0.x2 0 2.1006948 1.(Intercept) 1 4.2020874 1.x1 1 -0.4924303 1.x2 1 1.0917668 What I would

Loop linear regression and saving ALL coefficients

杀马特。学长 韩版系。学妹 提交于 2019-11-30 20:59:09
问题 Based on the link below, I created a code to run regression on subsets of my data based on a variable. Loop linear regression and saving coefficients In this example I created a DUMMY (0 or 1) to create the subsets (in reality I have 3000 subsets) res <- do.call(rbind, lapply(split(mydata, mydata$DUMMY),function(x){ fit <- lm(y~x1 + x2, data=x) res <- data.frame(DUMMY=unique(x$DUMMY), coeff=coef(fit)) res })) This results in the following dataset DUMMY coeff 0.(Intercept) 0 22.8419956 0.x1 0

R - merge a list of data frames into one data frame with missing values by row

北城余情 提交于 2019-11-30 13:51:15
问题 I have a variation on the oh-so-common problem of how to merge things together in R. I have a set of .txt files in a particular folder, and I have written a function that: makes a list of the files I want, and then for each file reads the file subsets the data (to extract just the rows and columns of interest) does some calculations on the data adds these new values to a list. What I end up with is a list with the following structure: >str(DataList) List of 16 $ :'data.frame': 14 obs. of 2

R - merge a list of data frames into one data frame with missing values by row

自闭症网瘾萝莉.ら 提交于 2019-11-30 08:36:19
I have a variation on the oh-so-common problem of how to merge things together in R. I have a set of .txt files in a particular folder, and I have written a function that: makes a list of the files I want, and then for each file reads the file subsets the data (to extract just the rows and columns of interest) does some calculations on the data adds these new values to a list. What I end up with is a list with the following structure: >str(DataList) List of 16 $ :'data.frame': 14 obs. of 2 variables: ..$ Sample: Factor w/ 14 levels "Sample_1A","Sample_1B",..: 1 2 3 4 5 6 7 8 9 10 ... ..$ Var1

How to pass “nothing” as an argument to `[` for subsetting?

吃可爱长大的小学妹 提交于 2019-11-27 14:23:00
I was hoping to be able to construct a do.call formula for subsetting without having to identify the actual range of every dimension in the input array. The problem I'm running into is that I can't figure out how to mimic the direct function x[,,1:n,] , where no entry in the other dimensions means "grab all elements." Here's some sample code, which fails. So far as I can tell, either [ or do.call replaces my NULL list values with 1 for the index. x<-array(1:6,c(2,3)) dimlist<-vector('list', length(dim(x))) shortdim<-2 dimlist[[shortdim]] <- 1: (dim(x)[shortdim] -1) flipped <- do.call(`[`,c

Trouble converting long list of data.frames (~1 million) to single data.frame using do.call and ldply

耗尽温柔 提交于 2019-11-27 11:56:22
I know there are many questions here in SO about ways to convert a list of data.frames to a single data.frame using do.call or ldply, but this questions is about understanding the inner workings of both methods and trying to figure out why I can't get either to work for concatenating a list of almost 1 million df's of the same structure, same field names, etc. into a single data.frame. Each data.frame is of one row and 21 columns. The data started out as a JSON file, which I converted to lists using fromJSON, then ran another lapply to extract part of the list and converted to data.frame and

Using cbind on an arbitrarily long list of objects

风流意气都作罢 提交于 2019-11-27 08:50:21
I would like to find a way to create a data.frame by using cbind() to join together many separate objects. For example, if A, B, C & D are all vectors of equal length, one can create data.frame ABCD with ABCD <- cbind(A,B,C,D) However, when the number of objects to be combined gets large, it becomes tedious to type out all of their names. Furthermore, Is there a way to call cbind() on a vector of object names, e.g. objs <- c("A", "B", "C", "D") ABCD <- cbind(objs) or on a list containing all the objects to be combined, e.g. obj.list <- list(A,B,C,D) ABCD <- cbind(obj.list) Currently, the only