3

I have let say 4 data sets f1,f2,i1,i2. I want to cbind() f1 with i1 and f2 with i2.

I can use

v1<-cbind(f1,i1)
v2<-cbind(f2,i2)

but I want to do this in some sort of loop.

I know the question is very basic. But after lots of searching I am still unable to find a solution for this.

Uwe
  • 41,420
  • 11
  • 90
  • 134
humera
  • 31
  • 2

2 Answers2

2

We can use Map to cbind the corresponding columns of both datasets

lst <- setNames(Map(cbind, mget(ls(pattern = "^f\\d+")),
        mget(ls(pattern = "^i\\d+"))), paste0("v", seq_along(f1)))

to create a list of datasets.

data

f1 <- data.frame(col1 = 1:5, col2 = 6:10)
f2 <- data.frame(col1 = 1:10, col2 = 11:20)
i1 <- data.frame(col3 = 11:15, col4 = 16:20)
i2 <- data.frame(col3 = 21:30, col4 = 31:40)
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662
  • thanks for your reply akrun. May be you can help me with other coding i have posted below.thanks – humera Sep 06 '17 at 12:35
  • Hello. I am looking to do the same thing, but I don't understand this code. I already have a list of the data frames I want to merge (a and a_1, b and b_1, etc.), but how do I merge the data sets and assign them to a variable in a loop? – Lisa Nov 19 '18 at 18:01
1

This is more simplistic:

Map(cbind,list(f1,f2),list(i1,i2))

This code should work

Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • 1
    v<-mapply(cbind, list(f1,f2),list(i1,i2), SIMPLIFY = FALSE) – humera Sep 06 '17 at 12:22
  • above code works the same as Onyambu.Thanks a lot members for answering.After generating a list of datasets called v. i have applied glm to the models – humera Sep 06 '17 at 12:24
  • Now i want to cross validate.I have split the data into test and train.My problem is that i have list of models ,test and training data.i want to apply predict function.but i am getting error .here is my coding . – humera Sep 06 '17 at 12:29
  • v<-mapply(cbind, int, fac, SIMPLIFY = FALSE) sample = sample.split(v, SplitRatio = .75) train = subset(v, sample == TRUE) test = subset(v, sample == FALSE) # Fitting Generalized Linear Models on data sets# models<-lapply(train,function(x)glm(EverBF ~ smokelesstobacco , family = binomial(link="logit"), data = x)) y.pred.im <- lapply(predict(models, newdata = test)) Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "list" – humera Sep 06 '17 at 12:32
  • Is you train and test data in numerical format? do you have your folds?? or you do not need them? – Onyambu Sep 06 '17 at 14:11
  • `Map(predict,models,test)` You have done a lot of `lapply` all this can be combined to one or two `mapply` statements.. – Onyambu Sep 06 '17 at 14:16
  • my test and train data consists of both numeric and categorical data – humera Sep 06 '17 at 14:19
  • by folds do you mean simulation?i will simulate the whole code at the end.but now problem is when i use this code > p= mapply(function(s, l) predict(s, newdata=l), models, testing, SIMPLIFY = FALSE) i get a list in which same value is predicted for all the observations.that is very confusing.it will be nice if someone shares his or her experience to solve this problem thanks – humera Sep 06 '17 at 14:29