Execution of multiple functions within loops *pply

Asked

Viewed 1,077 times

4

dput:

dataset=structure(list(var1 = c(93.6192987300456, 89.1180437617004, 
73.1943506933749, 
99.0350881963968, 32.3240431956947, 94.2517145164311, 53.7740966491401, 
64.1642983071506, 83.643307145685, 27.0992671512067, 84.45406595245, 
22.3189734295011, 49.4224909506738, 38.7974510155618, 33.9465192519128, 
30.2190610393882, 90.8118594065309, 40.8626275323331, 76.4244121313095, 
97.7241401933134, 63.1410409137607, 41.7930133081973, 24.7425195015967, 
68.4584231488407, 46.811489854008, 38.8751440867782, 59.3290254659951, 
67.7311567775905, 49.8230893351138, 68.9460825175047), var2 = 
c(87.7179634943604, 
67.9151312820613, 56.0685022361577, 27.2677147015929, 56.7003813013434, 
48.7404909729958, 44.7678123787045, 57.97383710742, 77.8373400121927, 
25.8361956849694, 44.5761758461595, 91.0274456255138, 91.8508980423212, 
84.0235557034612, 62.798991817981, 74.3813330866396, 44.5109844580293, 
25.9194917976856, 53.6176244169474, 57.4569314531982, 96.7371257580817, 
67.6377049833536, 77.6509994827211, 72.78081798926, 31.9740768149495, 
80.8034200407565, 64.3188071064651, 41.2181172892451, 60.2541393600404, 
90.3738238662481), var3 = c(95.3002750501037, 98.9393053390086, 
48.2393125072122, 36.4915966801345, 45.5738344974816, 28.4179201349616, 
70.0430107302964, 57.5289596244693, 26.5800921246409, 48.322719912976, 
63.2224931940436, 65.5816095136106, 64.094599634409, 83.5935281775892, 
61.6953182034194, 59.7588985785842, 63.2139686867595, 49.167671110481, 
76.7882913909853, 85.1037272438407, 28.9329998753965, 85.7834707945585, 
86.9987625069916, 28.9337033219635, 25.9433646500111, 25.1355851069093, 
70.9878898411989, 81.0662723146379, 65.1459879614413, 76.2660158798099
), var4 = c(41.730745472014, 34.5881148800254, 68.3925887942314, 
30.5000455118716, 73.9748882688582, 80.0406797043979, 86.2524883449078, 
93.9809632487595, 51.7102645710111, 31.0985423251987, 46.0413429327309, 
20.091195832938, 43.4606329724193, 70.5541634373367, 25.8027635701001, 
55.15690010041, 38.7906171381474, 72.2386035695672, 46.2779484130442, 
42.6929569430649, 41.5135116875172, 66.9485157355666, 67.9324744641781, 
31.3458665274084, 73.5289090685546, 48.2540292851627, 25.7967964001, 
65.6544559262693, 74.1149041801691, 22.1287422254682), var5 = c(90.5113583803177, 
32.5118893012404, 83.1625051796436, 90.591189134866, 77.1059499308467, 
21.4595774561167, 49.0021525137126, 51.5211039595306, 73.1922925822437, 
39.9916738457978, 47.6714463345706, 45.2438995614648, 71.3770489767194, 
82.4075744301081, 41.4439569972456, 29.7904395870864, 76.6024437360466, 
87.2250938042998, 74.4304648041725, 99.0381432883441, 53.5259937494993, 
74.8778975568712, 74.7214529849589, 20.3414874337614, 91.975539047271, 
44.2561679519713, 37.8161336667836, 98.98452071473, 69.0480253845453, 
40.6559890136123), var6 = c(32.9364681430161, 36.5663177333772, 
44.8065084964037, 86.2597990036011, 20.5922558158636, 40.084039978683, 
58.9583773910999, 64.1501008719206, 84.0120720304549, 30.7220995798707, 
95.7582007162273, 78.4770882315934, 36.9673660025001, 23.1071223691106, 
88.2986530847847, 52.1857565641403, 72.7651609480381, 50.2782971039414, 
39.2872701212764, 60.7792820967734, 36.8851349875331, 67.3444269225001, 
94.3507463112473, 64.2956222966313, 32.9426118545234, 60.2395292185247, 
24.0946758165956, 95.1894435845315, 72.3093654960394, 42.4603571370244
)), class = "data.frame", row.names = c(NA, -30L))

Based on this data, please consider the following procedures::

Creation of the function

myfun<-function(x){
    c(sum=sum(x),
    mean=mean(x),
    sd=sd(x),
    q=quantile(x,seq(0,1,.25)))
}

Parsing 1

# execução da função em um dataframe
lapply(dataset,myfun)

And the result is ok.

Parse 2

But for a list this does not occur:

# criação da lista
mylist<-list(dataset[,c(1:3)],dataset[,c(4:6)])

# execução da função em uma lista
lapply(mylist,myfun)

Error in is.data.frame(x) : (list) Object cannot be coerced to type 'double'

Then I tried to run by the conventional method:

Analysis 3

# para um dataframe
lapply(dataset,function(x){
    s=sum(x)
    m=mean(x)
    s=sd(x)
    q=quantile(x,seq(0,1,.25))
})

And the function is executed only for quantis, the last function I wrote.

Analysis 4

For a list no results occur:

# para uma lista
lapply(mylist,function(x){
    s=sum(x)
    m=mean(x)
    s=sd(x)
    q=quantile(x,seq(0,1,.25))
})

And the error is the same as "Analysis 2":

Error in is.data.frame(x) : (list) Object cannot be coerced to type 'double'

Questions:

  • What’s wrong with the job "Parse 2"?
  • What are the problems in the functions "Analysis 3" and "Analysis 4"?

1 answer

5


The function lapply accepts a single data frame (or list) at a time. Note that however much you enter with a data.frame the function converts the object into a lista. See the function input lapply:

a vector (atomic or list) or an expression object. Other objects (including classed objects) will be coerced by base::as.list.

Therefore the Parsing 1 works and the Parse 2 no. To operate the Parse 2, it is necessary:

lapply(mylist, function(x){lapply(x, myfun)})

That is, for each list, within each list, apply the function.

To Analysis 3 only returns the quartiles, because as you did not concatenate them into a single object, either via c() or data.frame, the function will return only the last element.

To Analysis 4 does not work for the same reason as Analysis 2 and 3. The code below does what you need:

lapply(mylist, function(x){ lapply(x, function(x) { s=sum(x)
                                   m=mean(x)
                                   s=sd(x)
                                   q=quantile(x,seq(0,1,.25))})
})
  • A doubt, William: why in the last job you wrote you need to apply twice function(x)? And, the keys inside functions apply serve as a concatenator? That is, instead of c(...) can I use {...}, at all times?

  • 1

    You need the function(x) and as a consequence of { in the family *pply whenever your output is more than 1 value or when you need to apply more than 1 function. A {is used to input its function, and you use the c(...) to contact objects and return a single output object (as it cannot return more than 1).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.