Most voted "apply" questions
In R this function returns a vector or matrix or list of values obtained by applying a function in the margins or dimensions of a matrix. The apply function has advantages over loops in terms of code simplicity and speed.
Learn more…18 questions
Sort by count of
-
16
votes2
answers4769
viewsApply, sapply, mapply, lapply, vapply,rapply, tapply, replicate, Aggregate, by and correlates in R. When and how to use?
What is the difference between the functions apply, sapply, mapply, lapply, vapply, rapply, tapply, replicate, aggregate, by and correlates in the R? When and how to use each of them? There are…
-
8
votes2
answers389
views*apply with three-argument functions in R
I created a function of three arguments and I want to apply it to a Matrix 200 X 3 where in each line are the three arguments I need to use in the function. As a result I would get a vector of size…
-
7
votes2
answers87
viewsSomatoria accumulating columns in a Matrix in R
I have the following situation : In a numeric type Matrix : Temp <- matrix(runif(25, 10, 100),nrow=5,ncol=5) V1 V2 V3 V4 V5 11 34 45 54 55 16 21 45 75 61 88 49 85 21 22 12 13 12 11 10 69 45 75 78…
-
5
votes1
answer70
viewsHow do I exchange a apply inside a for for for a double apply?
I have a vector origem and a vector destino with different locations in latitude and longitude. For each location in origem, I want to count how many places in destino are located in a radius of up…
-
5
votes2
answers651
viewsHow to perform the tapply function for multiple dataframes in R?
I wish to perform only ONE function tapply and get results for each of the dataframes at once: dataset1<-data.frame(group = rep(c('a','b','c','d'), 3, each = 3), number1 = c(1:36), number2 =…
-
5
votes3
answers969
viewsDifferences and similarities between apply and for loop functions
I have this list: dataset<-data.frame(matrix(runif(6*30,20,100),ncol=6)) cluster<-kmeans(dataset,centers=3) cluster dataset$kmeans<-as.factor(cluster[['cluster']])…
-
4
votes1
answer723
viewsApply a function using some columns of all rows of a Dataframe (r)
I have the following Dataframe : AVG_VOLUME AVG_RETURN VOL PRICE SPX Index 500000 0.01 0.08 2082 KR7000270009 6000 0.02 0.09 102 KR7005930003 7000 0.02 0.08 103 JP3266400005 8000 0.03 0.08 104…
-
3
votes1
answer86
viewsWhat is the logic of Function(x) and Function(x,y) in R?
In the execution of loops with functions apply, is always used function(x). Example: lapply(df,function(x)gsub('[^A-Z]','',x)) In other functions, however, it is used function(x,y). Example:…
-
2
votes1
answer64
viewsHow to use xmlSApply with separator?
I used the xmlSApply and would like to get the result with a separator but did not know how to apply. Below is an example for you to reproduce: library(XML) fileUrl <-…
-
2
votes2
answers91
viewsAdding the same substring to multiple columns in R
I have a base with 2 lines and 68 remarks called "varnomes" and would like to add the word "PF" at the end of each remark. With that, I tried to use the function Paste:…
-
2
votes1
answer61
viewsIs it possible to convert this for loop to an apply family function?
df_loop <- data.frame(data1, data2, data3, 0) for (i in 4:nrow(df_loop[1])) { tmp_1 = df_loop[i,1] tmp_2 = df_loop[i,2] tmp_2_lag3 = df_loop[i-3,2] tmp_3 = df_loop[i,3] if (!is.na(tmp_2_lag3)) {…
-
2
votes1
answer58
viewsPreserve rownames after use of apply family functions
I make use of the functions apply and, I need you to rownames stay in the database after execution of these. Consider: dataframe=structure(list(Composição100g = structure(c("Valor energético (KJ)",…
-
2
votes1
answer40
viewsApply function, extract regression data
I have the following sample: x <- structure(list(POP = structure(c(1L, 12L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L, 14L), .Label = c("pop1", "pop10",…
-
1
votes1
answer54
viewsHow to send data from data frames in optimized R code questions?
Edited - As pointed out in Marcos Moraes' comment, I tried to reinvent the wheel of a resource available in the base layer of R, since dput is able to produce the same effect. Fault ours. : ( The…
-
1
votes3
answers226
viewsAverage temperature in R
Have: Data Sala.1 Sala.2 Sala.3 Horas 1 02/08/2013 20.5 19.7 21.6 15:00 2 02/08/2013 19.7 18.9 20.2 15:30 3 02/08/2013 19.7 19.1 20.0 16:00 4 02/08/2013 19.7 18.8 19.8 16:30 5 02/08/2013 19.8 18.8…
-
1
votes2
answers3207
viewsReplace specific column values with NA
I would like to detect and replace values above 6 in just a few columns of a data.frame by NA. I made the syntax like this, but it is giving error... Could someone give me a hand? Thank you! data…
-
0
votes2
answers60
viewsR Regression by condition using apply or for
I have the following sample: x <- structure(list(POP = structure(c(1L, 12L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L, 14L), .Label = c("pop1", "pop10",…
-
0
votes0
answers14
viewsHow to use call, apply and bind?
Can someone demonstrate/explain how to use call, apply and bind methods in javascript? Simple examples and objectives are very welcome! Further explanations also :D Extra points for those who can…