Things that can be done by simply setting parameters are not available.
In the process of using R, you will always encounter such a type of functions. The number of parameters they accept can be arbitrary. The function will process these parameters and return the processing result.
The simplest example is
for example:
> x1 = 1:10 > x2 = 11:20 > x3 = 21:30 > (x1,x2,x3) x1 x2 x3 1 1 11 21 2 2 12 22 3 3 13 23 4 4 14 24 5 5 15 25 6 6 16 26 7 7 17 27 8 8 18 28 9 9 19 29 10 10 20 30
You can add as many vector parameters to the function (x1, x2, x3 are vectors).
But the current situation is: you know clearly that you only need to piece together these three vectors into a data frame. Then, it is the best way to write it as (x1, x2, x3), and there is no need to write it as follows:
> ("",list(x1,x2,x3)) X1.10 X11.20 X21.30 1 1 11 21 2 2 12 22 3 3 13 23 4 4 14 24 5 5 15 25 6 6 16 26 7 7 17 27 8 8 18 28 9 9 19 29 10 10 20 30
However, suppose you are facing this: You now need to read all rows of data from a file on disk, but as the situation changes, the length of the file will change.
But you plan to write a program that can deal with files of various lengths at the same time. The purpose of the program is to vertically put the contents of each line in the file and form a data frame in columns.
So what methods do you have? ——()+t(), OK, I admit I lost again, it seems that it is not the best option.
So what if the types of lines of this file are different? For example, if one line of characters, one line of numbers, one line of boolean values, and such a loop extension, what can you do?
f = file("", "r") n = length(("")) / 3 l = list() for (i in 1:n) { l[[(i-1)*3 + 1]] = scan(file = f, sep = ",", nlines = 1, what = "", quiet = TRUE) l[[(i-1)*3 + 2]] = scan(file = f, sep = ",", nlines = 1, what = 0, quiet = TRUE) l[[(i-1)*3 + 3]] = scan(file = f, sep = ",", nlines = 1, what = TRUE, quiet = TRUE) } names(l) = paste("l", 1:length(l), sep = "") r = ("", l) print(r)
There are still alternatives:
(1) I just use ()+t(), and at worst I can convert the type according to the column later!
(2) It's still the above loop. I don't want to mount the value into the list every time. I create it directly, and then add it column by column using cbind(), so there is no need
Then, now increase the difficulty again: cancel the use of the transpose function t(), and the cbind() function is not allowed. Then you can only use it.
I'm actually not arrogant at all. I just need to change the situation - the ffbase package, an expansion package that specializes in handling big data. The ffdf object is similar (but can accommodate more data), but it is not easy to add new columns and cannot be transposed! You don't need to know what the ffdf function is, you just need to know it and you can add as many parameters as you want.
OK, here is a program snippet involving the ffbase package
Let's experience the usage:
addStrategyData <- function(detailList, index) { a = list() x = detailList[[index]] vMode = sapply(names(x), function(y) switch(y, "s" = "integer", "t" = "double", "f" = "logical")) names(vMode) = names(x) x = (x, vmode = vMode) for (i in 1:ncol(x)) a[[i]] = x[[i]] for (i in (length(a) + 1:length(detailList))) a[[i]] = ff(FALSE, length = nrow(x), vmode = "logical") a[[ncol(x) + index]] = ff(TRUE, length = nrow(x), vmode = "logical") names(a) = c(names(x), paste("S", 1:length(detailList), sep = "")) return(("ffdf", a)) }
In some cases, you know that a function accepts clear numbers of parameters, but it is too many, you are lazy, so you use it; but in more cases, you have to use it.
Supplement: What is the difference between LAPPLY and R?
Recently I'm learning R, the two functions lapply and are confused. It seems that they are similar to map functions in Lisp. But why are there two different names? Why doesn't R use a function called map?
There is a function called Map, which may be similar to maps in other languages:
lapply
Returns a list of the same length as X, where each element is the result of applying FUN to the corresponding element of X.
Construct and execute a function call to it, passing it from a name or a function and a list of arguments.
Map applies a function to the corresponding element of a given vector... Map is a simple mapply wrapper that does not attempt to simplify the result, similar to the mappcar of Common Lisp (but the parameters are recycled). Future versions may allow some control over the result types.
1. Map is a mapply packaging
2. Lapply is maply
3. Therefore, in many cases Map and lapply will be similar.
For example, here is lapply:
lapply(iris, class) $ [1] "numeric" $ [1] "numeric" $ [1] "numeric" $ [1] "numeric" $Species [1] "factor"
and use Map:
Map(class, iris) $ [1] "numeric" $ [1] "numeric" $ [1] "numeric" $ [1] "numeric" $Species [1] "factor"
Take a function as input and splash other parameters on the function. For example, it is widely used to assemble lists into simpler structures (usually using rbind or cbind).
For example:
x <- lapply(iris, class) (c, x) Species "numeric" "numeric" "numeric" "numeric" "factor"
laply applies a function on the list and calls a function with the parameter list. This looks very different to me
Take an example with a list:
X <- list(1:3,4:6,7:9)
Using lapply you can get the meaning of each element in the list:
> lapply(X,mean) [[1]] [1] 2 [[2]] [1] 5 [[3]] [1] 8
Given an error, as it means that the parameter "trim" is 1.
On the other hand, rbind binds all parameters. So bind X line, you do:
> (rbind,X) [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9
If you use laply, R will apply rbind to every element in the list, giving you this nonsense:
> lapply(X,rbind) [[1]] [,1] [,2] [,3] [1,] 1 2 3 [[2]] [,1] [,2] [,3] [1,] 4 5 6 [[3]] [,1] [,2] [,3] [1,] 7 8 9
To have something like Map, you need?mapply, which is a completely different thing. To get the average of each element in X, for example, but using a different trimming, you can use:
> mapply(mean,X,trim=c(0,0.5,0.1)) [1] 2 5 8
lapply is similar to map, not. lapply applies the function to all elements of the list, calling a function where all function parameters are in the list. So for a list of n elements, lapply has n function calls, and there is only one function call. So it's completely different from lapply. Hope this clarifies your question.
A code example:
(sum, list(c(1,2,4,1,2), = TRUE))
and:
lapply(c(1,2,4,1,2), function(x) x + 1)
In the simplest terms:
laply() applies a given function for each element in the list, so there will be several function calls.
() applies the given function to the list as a whole, so there is only one function call.
The best way to learn is to use function examples in R documentation.
lapply() is a map-like function. () is different. It is used to pass parameters to functions that list forms instead of enumerating them. For example,
> ("+",list(4,5)) [1] 9
Although there are many answers, here are my examples for reference. Suppose we have a data list:
L=list(c(1,2,3), c(4,5,6))
The function lapply returns a list.
lapply(L, sum)
The above means just like the following.
list( sum( L[[1]]) , sum( L[[2]]))
Now let's do the same thing
(sum, L)
It means
sum( L[[1]], L[[2]])
In our example, it returns 21. In short, lapply always returns a list, and the return type actually depends on the function executed.
The difference between the two is:
lapply(1:n,function,parameters)
=>This sends 1, parameter to function=>This sends 2, parameter to function, etc.
Just send 1 … n as a vector and parameter to run
So in the application you have n function calls, in which you only have one
I feel like an important aspect of not getting certificate (or not obvious to me). That is, you can pass the named parameter in the list to the function using it.
For example, runif requires the parameters n , min and max . This information can be passed using it as shown below.
para <- list(n = 10, min = -1, max = 1) (runif, para) #[1] -0.4689827 -0.2557522 0.1457067 0.8164156 -0.5966361 0.7967794 #[7] 0.8893505 0.3215956 0.2582281 -0.8764275
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.