apply

R: How to create multiple maps (rworldmap) using apply?

你说的曾经没有我的故事 提交于 2019-12-20 05:47:16
问题 I want to create multiple maps (similar to this example) using the apply family. Here a small sample of my code (~200 rows x 150 cols). (UN and ISO3 are codes for rworldmap): df <- structure(list(BLUE.fruits = c(12803543, 3745797, 19947613, 0, 130, 4), BLUE.nuts = c(21563867, 533665, 171984, 0, 0, 0), BLUE.veggies = c(92690, 188940, 34910, 0, 0, 577), GREEN.fruits = c(3389314, 15773576, 8942278, 0, 814, 87538 ), GREEN.nuts = c(6399474, 1640804, 464688, 0, 0, 0), GREEN.veggies = c(15508,

R apply error - error in as.matrix.data.frame()

霸气de小男生 提交于 2019-12-20 05:31:21
问题 i am encountering a baffling error. i am using the following function to delete rows of a dataframe containing an NA observation in any column ##### removes NA'd rows from a dataFrame wipeNArows<-function(X){ rowsToDelete<-unique(unlist(apply(apply(X,2,is.na),2,which))) if (length(rowsToDelete)>0){ return (X[-rowsToDelete,]) } else{ return (X) } } This function works fine normally, for instance a reproducible example is: testFrame<-data.frame(x=rpois(20,10),y=rpois(20,10),z=rpois(20,10))

For-Loop By Columns with existing For-loop by Rows

拥有回忆 提交于 2019-12-20 05:21:52
问题 I have a dataset as follows as a sample. My actual dataset has 5000 columns: # Define Adstock Rate adstock_rate = 0.50 lag_number = 3 # Create Data advertising = c(117.913, 120.112, 125.828, 115.354, 177.090, 141.647, 137.892, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 158.511, 109.385, 91.084, 79.253, 102.706, 78.494, 135.114, 114.549, 87.337, 107.829, 125.020, 82.956, 60.813, 83.149, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 129.515, 105.486, 111.494, 107.099, 0

using lapply function and list in r

若如初见. 提交于 2019-12-20 04:22:07
问题 d1 <- data.frame(col_one = c(1,2,3),col_two = c(4, 5, 6)) d2 <- data.frame(col_one = c(1, 1, 1), col_two = c(6, 5, 4)) d3 <- data.frame(col_one = c(7, 1, 1), col_two = c(8, 5, 4)) my.list <- list(d1, d2,d3) for (i in 1:3) { table<- lapply(my.list, function(data, count) { sql <- #sqldf( paste0( "select *,count(col_one) from data where col_one = ", count," group by col_one" ) #) print(sql) }, count = i) } output: [1] "select *,count(col_one) from data where col_one = 1 group by col_one" [1]

Processing the list of data.frames with “apply” family of functions

爷,独闯天下 提交于 2019-12-20 04:16:02
问题 I have a data frame which I then split into three (or any number) of dataframes. What I’m trying to do is to automatically process each column in each dataframe and add lagged versions of existing variables. For example if there were three variables in each data.frame (V1, V2, V3) I would like to automatically (without hardcoding) add V1.lag, V2.lag and V3.lag. Here is what I have so far, but I’m stuck now. Any help would be highly apprecaited. dd<-data.frame(matrix(rnorm(216),72,3),c(rep("A"

split on factor, sapply, and lm [duplicate]

瘦欲@ 提交于 2019-12-20 01:41:08
问题 This question already has answers here : Linear Regression and group by in R (10 answers) Closed 3 years ago . I want to apply lm() to observations grouped by subject, but cannot work out the sapply syntax. At the end, I want a dataframe with 1 row for each subject, and the intercept and slope (ie, rows of: subj, lm$coefficients[1] lm$coefficients[2]) set.seed(1) subj <- rep(c("a","b","c"), 4) # 4 observations each on 3 experimental subjects ind <- rnorm(12) #12 random numbers, the

How to bind function arguments

非 Y 不嫁゛ 提交于 2019-12-20 01:34:48
问题 How do I partially bind/apply arguments to a function in R? This is how far I got, then I realized that this approach doesn't work... bind <- function(fun,...) { argNames <- names(formals(fun)) bindedArgs <- list(...) bindedNames <- names(bindedArgs) function(argNames[!argNames %in% bindedArgs]) { #TODO } } Thanks! 回答1: Have you tried looking at roxygen's Curry function? > library(roxygen) > Curry function (FUN, ...) { .orig = list(...) function(...) do.call(FUN, c(.orig, list(...))) }

JavaScript 中 call()、apply()、bind() 的用法与区别

落花浮王杯 提交于 2019-12-19 20:37:02
JavaScript 中 call()、apply()、bind() 的用法与区别 在js的开发当中,会经常使用到 call() 、 apply() 、 bind() 这三个函数,这三个函数到底是干嘛的呢? 其实很简单,这其中最主要的区别是 this 的指向问题,这三个函数都是改变函数内部 Function 的 this 指向。 首先,我们先看一下下面的例子: // 例子一 var myDog = 'tom' var isObj = { name : this . myDog , color : 'red' , itSay : function ( ) { console . log ( 'my name is ' + this . myDog + ', color is ' + this . color ) } } console . log ( isObj . name ) // tom console . log ( isObj . itSay ( ) ) // my name is undefined, color is red 我们可以看到上面的打印区域, isObj.name 打印出来是 tom ,但是下面 itSay() 打印输出 undefined 。因为在上面打印之中 this 指向的是 window 对象, myDog 是一个全局变量,相当于 isObj

Rolling a function on a data frame

纵饮孤独 提交于 2019-12-19 07:29:29
问题 I have the following data frame C . >>> C a b c 2011-01-01 0 0 NaN 2011-01-02 41 12 NaN 2011-01-03 82 24 NaN 2011-01-04 123 36 NaN 2011-01-05 164 48 NaN 2011-01-06 205 60 2 2011-01-07 246 72 4 2011-01-08 287 84 6 2011-01-09 328 96 8 2011-01-10 369 108 10 I would like to add a new column, d , where I apply a rolling function, on a fixed window (6 here), where I somehow, for each row (or date), fix the value c . One loop in this rolling function should be (pseudo): a b c d 2011-01-01 0 0 NaN a

How do I add random `NA`s into a data frame

会有一股神秘感。 提交于 2019-12-19 06:06:14
问题 I created a data frame with random values n <- 50 df <- data.frame(id = seq (1:n), age = sample(c(20:90), n, rep = TRUE), sex = sample(c("m", "f"), n, rep = TRUE, prob = c(0.55, 0.45)) ) and would like to introduce a few NA values to simulate real world data. I am trying to use apply but cannot get there. The line apply(subset(df,select=-id), 2, function(x) {x[sample(c(1:n),floor(n/10))]}) will retrieve random values alright, but apply(subset(df,select=-id), 2, function(x) {x[sample(c(1:n)