NelsonGon
NelsonGon

Reputation: 13319

Understanding l_ply from plyr

From the documentation:

All output is discarded. This is useful for functions that you are calling purely for their side effects like displaying plots or saving output.

I've spent some time playing around and trying to find a suitable use case but haven't(yet).

Looking at the examples hasn't helped me better understand it.

Sample usage:

l_ply(iris[1:5,1], function(x) print(summary(x)))

This will work.

However, under what circumstances might one need to print and then discard these results?

Upvotes: 1

Views: 165

Answers (1)

phoxis
phoxis

Reputation: 61910

Consider the following

X <- matrix (c (rnorm (50)), ncol = 5);

Assume each colmn of X indicates a series which you want to overplot. You can do it as following, by first creating an empty plot and then plotting the series corresponding to each column, using lapply. Although lapply will return the values returned by the plot call that we do not want.

plot (NULL, ylim = range (X), xlim = c (1, nrow (X)));
lapply (1:ncol (X), function (i) points (X[,i], type = "o", col = i));

Instead you can use

plot (NULL, ylim = range (X), xlim = c (1, nrow (X)));
l_ply (1:ncol (X), function (i) points (X[,i], type = "o", col = i));

This has the same effect but does not return the values returned by plot. Here, the "side effect" is the plot function plotting on the device.

Upvotes: 4

Related Questions