본문 바로가기
반응형

R71

(R) split function/ sapply--> tapply split function divides the data in the vector x into the groups defined by function. There are two main arguments in split function. x: a vector or a data frame f: a factor or a list of factors >?split > list_1=list(x=rnorm(5,0,1),y=runif(3,5,10))> list_1$x[1] -0.1205420 0.6790684 -1.0631887 0.7876966 0.3130599 $y[1] 8.521317 5.693054 5.061110 > sapply(list_1,mean) x y 0.1192188 6.4251602 > str(.. 2020. 11. 17.
(R) tapply function/ comparision to aggregate function /permutation test with tapply function the tapply function applies a function to each group of values given by a unique combination of the levels of certain factors. >?tapply the apply function needs three main arguments which are x, INDEX, FUN.x : typically vector which we want to apply functions to. INDEX: a list of one or more factors, each of same length as x. fun: a function to be applied Depending on the number of factors in th.. 2020. 11. 17.
(R) lapply, sapply, mapply/ two-sample t-test using mapply function lapply function always returns the result of function as a list from a list. sapply function always returns the result of functions as a vector from a list. > list_1=list(x=rnorm(5,0,1),y=runif(3,5,10))> list_1$x[1] -0.1718824 -0.1953697 -0.3611449 1.1755966 -1.0458594 $y[1] 7.881664 9.084847 9.950752 > list_2 = list(x = rnorm(10,0,1), y = c(rnorm(5,3,8), NA)) > list_2$x [1] 1.76547974 -0.748861.. 2020. 11. 17.
(R) apply function on matrix and data frame There are some functions provided by R to calculate statistics for rows or columns for matrix. columns, rowSums, colMeans, rowMeans are the things. > x = matrix(runif(20,min=2,max=35), 3) > x [,1] [,2] [,3] [,4] [,5] [,6][1,] 25.49332 29.93656 8.329258 9.427876 6.314251 11.469574[2,] 32.90328 25.86397 25.692699 23.865729 32.419499 33.890851[3,] 17.61071 20.22315 33.823737 18.290469 4.744376 6.37.. 2020. 11. 15.
반응형