function

Julia scoping: why does this function modify a global variable?

随声附和 提交于 2021-01-27 22:51:45
问题 I'm a relative newcomer to Julia, and so far I am a fan of it. But coming from years of R programming, some scoping rules leave me perplexed. Let's take this function. This behaves exactly as I would expect. function foo1(x) y = x t = 1 while t < 1000 t += 1 y += 1 end return 42 end var = 0; foo1(var) # 42 var # 0 But when doing something similar on an array, it acts as a mutating function (modifies it argument in the global scope!) function foo2(x) y = x t = 1 while t < 1000 t += 1 y[1] += 1

How to Apply functions to specific set of columns in data frame in R to replace NAs

我与影子孤独终老i 提交于 2021-01-27 21:50:55
问题 I have a data set in which I want to replace NAs in different columns differently. Following is the dummy data set and code to replicate it . test <- data.frame(ID = c(1:5), FirstName = c(NA,"Sid",NA,"Harsh","CJ"), LastName = c("Snow",NA,"Lapata","Khan",NA), BillNum = c(6:10), Phone = c(1213,3123,3123,NA,NA), Married = c("Yes","Yes",NA,"NO","Yes"), ZIP = c(1111,2222,333,444,555), Gender = c("M",NA,"F",NA,"M"), Address = c("A","B",NA,"C","D")) > test ID FirstName LastName BillNum Phone Married

JS: Default function parameter values and scope

感情迁移 提交于 2021-01-27 21:33:56
问题 I'm a bit confused about how scope & variable assignment within a function seems to change when assigning a default parameter value for that function. For example, when this function has a default value assigned to parameter i , the output array variable appears to be block-scoped when inspected with Chrome Dev Console,: function steps(n, i = 40) { var output = [n]; } steps(10, 20); However, by removing the default parameter value for i , the output array variable is scoped locally: function

Optimize/Vectorize Database Query with R

会有一股神秘感。 提交于 2021-01-27 19:42:14
问题 I am attempting to use R to query a large database. Due to the size of the database, I have written the query to fetch 100 rows at a time My code looks something like: library(RJDBC) library(DBI) library(tidyverse) options(java.parameters = "-Xmx8000m") drv<-JDBC("driver name", "driver path.jar") conn<- dbConnect( drv, "database info", "username", "password" ) query<-"SELECT * FROM some_table" hc<-tibble() res<-dbSendQuery(conn,query) repeat{ chunk<-dbFetch(res,100) if(nrow(chunk)==0){break}

Is in case of R functions a parent's parent environment of an environment also parent to the environment?

房东的猫 提交于 2021-01-27 19:28:29
问题 Assume, I create a parent's parent environment (E2 <= environment E1 <= environmet E) of an environment (E). My questions is, whether E2 is also parent to E. To me it was clear, that the answer was yes, but the following code seems to state the opposite. What is wrong? See the last line in the reproducible example below, where print(parent-parent ) yields # [1] FALSE # I would expect a TRUE here!! From the docs ?parent.env If one follows the chain of enclosures found by repeatedly calling

Implementing pandas function to numpy functions

ε祈祈猫儿з 提交于 2021-01-27 19:20:44
问题 Is there a way I could convert the xy_mean function to be computed using the pandas library just like the y_mean function. I found out that the pandas function Y_mean = pd.Series(PC_list).rolling(number).mean().dropna().to_numpy() is way faster than the numpy version ym = (np.convolve(PC_list, np.ones(shape=(number)), mode='valid')/number)[:-1] . The equation for the xy_mean would be ((index of value)*value + (index of value)*value)/number The index number would be dependent on the variable

control flow for calling other columns in function

我与影子孤独终老i 提交于 2021-01-27 18:50:17
问题 I'm trying to make a connection to other columns within a function given a conditional. Essentially, I want to make a dataframe go from long to wide given a conditional, where those values in one column are NA relative to another column that has values in that same row, turn the NAs into a specific numeric. Although the values assigned have to be column specific. So if 2010 has NAs whilst 2019 has a value, then return 16 otherwise, if 2019 has NAs when in that same row, 2010 has values,

Find the maximum of the function in R

时光总嘲笑我的痴心妄想 提交于 2021-01-27 17:09:44
问题 I have the following function. Let F(.) is the cumulative distribution function of the gamma distribution with shape = 1 and rate =1 . The denominator is the survival function S(X) = 1 - F(X) . The g(x) is the mean residual life function. I wrote the following function in r. x = 5 denominator = 1 -pgamma(x, 1, 1) numerator = function(t) (1 - pgamma(t, 1, 1)) intnum = integrate(numerator , x, Inf) frac = intnum$value/denominator frac How can I find the maximum of the function g(x) for all

Can Postgres use a function in a partial index where clause?

谁说胖子不能爱 提交于 2021-01-27 13:29:55
问题 I have a large Postgres table where I want to partial index on 1 of the 2 columns indexed. Can I and how do I use a Postgres function in the where clause of a partial index and then have the select query utilize that partial index? Example Scenario First column is "magazine" and the second column is "volume" and the third column is "issue". All the magazines can have same "volume" and "issue" #'s but I want the index to only contain the two most recent volumes for that magazine. This is

Pass a function as a parameter in Netlogo

两盒软妹~` 提交于 2021-01-27 13:20:33
问题 In many other programming languages, you can pass a function as an argument to another function and call it from within the function. Is there anyway to do this in Netlogo? Such as the following: ;; x,y,z are all ints to-report f [x y z] report x + y + z end ;; some-function is a function ;; x y and z are ints to-report g [some-function x y z] report (some-function x y z) + 2 end to go show g f 1 2 3 end This would be a nice feature. I'm trying to implement an abstract local search algorithm