Declaring a Const Variable in R

Declaring a Const Variable in R

See lockBinding:

a <- 1
lockBinding("a", globalenv())
a <- 2
Error: cannot change value of locked binding for 'a'

Making a Variable Constant in a Function in R

A possible solution is to define your function within another function:

g <- function( index ){
function( x ) x + index
}
index <- 3
f <- g( index )
f(4)
index<-20
f(4)

Now the output of g( index ) is a function which is defined within the (execution) environment of g. This function (f) will look at the value of indexin this environment, where it is fixed to 3. That's why it works, but maybe there is a simpler solution.

How do we set constant variables while building R packages?

One approach would be to use R's options interface. Create a file zzz.r in the R directory (this is the customary name for this file) with the following:

.onLoad <- function(libname, pkgname) {
options(api_path='...', username='name', password='pwd')

}

This will set these options when the package is loaded into memory.

How to define a constant in R

Make C = 1 for when you compute the integral of the function. For that, you can make it an optional argument to your function with a default value:

f <- function(x, C = 1) C * x * exp(-x)

Then, compute:

z <- integrate(f, lower = 0, upper = Inf)

For the integral to be 1 with the real value for C, you need C * z == 1, i.e.:

C <- 1 / z$value
C
# [1] 1

As it turns out, the integral z is already equal to 1 so picking C = 1 was a lucky choice. You have nothing to do and you can just start using f as-is. Had it not been the case, I would have suggested to redefine f:

f_final <- function(x) f(x, C = 1 / z$value)

(Regarding your second question, you just had to look at the documentation for ?integrate and refer to the "Value" section.)

Why can't I initialize a const variable with a primitive value received as a function argument in Dart?

Dart constant variables must be initialized with compile-time constant expressions.
A compile-time constant expression must always have the same value—precisely one value per source location.

Dart doesn't have "constant values" as such. It has constant expressions, which are known to evaluate to precisely one value, and for which it's possible to know this value at compile-time. That allows the compiler to canonicalize those constants values, so different constant expressions evaluating to constant objects with the same state are canonicalized to be the same object.

Your amount variable is not a compile-time constant expression. It can have different values at different times (because it's a function parameter and people might call the function with different arguments), so it cannot be a constant expression.

And therefore it cannot be used to initialize a constant variable, because constant variables can only have one value.

void test(int amount) {
const _amount = amount;
const list = [_amount]; // <- MUST ALWAYS HAVE SAME VALUE
}

In short: Dart constant variables must be initialized with a compile-time constant expression. A constant expression must always have the same value. This is the fundamental rule about Dart constant expressions which most other restrictions are derived from. (For example, a constant variable being used is a constant expression, so it must always be bound to the same value, which is why it must be initialized with a constant expression.)

Which variables in dataset are constant within id

Let's pretend that mtcars$carb is our id. Then, for each number of each id, we want to find out how many distinct gear values there are:

data.table(mtcars)[, lapply(.SD, function(x) length(unique(x))), by=carb]

Produces

   carb mpg cyl disp hp drat wt qsec vs am gear
1: 4 8 2 8 7 8 9 10 2 2 3
2: 1 7 2 7 6 6 7 7 1 2 2
3: 2 9 2 10 8 9 10 10 2 2 3
4: 3 3 1 1 1 1 3 3 1 1 1
5: 6 1 1 1 1 1 1 1 1 1 1
6: 8 1 1 1 1 1 1 1 1 1 1

Any variable with value > 1 has changing values for each carb value.


Edit:

Alternatively, we could expand this by running:

data.table(mtcars)[,lapply(.SD,uniqueN),by=carb
][,!"carb"][,lapply(.SD,table)]

mpg cyl disp hp drat wt qsec vs am gear
1: 2 3 3 3 3 2 2 4 3 3
2: 1 3 1 1 1 1 1 2 3 1
3: 1 3 1 1 1 1 1 4 3 2
4: 1 3 1 1 1 1 2 2 3 3
5: 1 3 3 3 3 1 2 4 3 1

This has the advantage (not apparent with this sample data set) that a given column is constant within the ID (carb here) ONLY IF the output of the above code is constant and equal to length(unique(id)) in the corresponding column. There are 6 values of carb, so we can see that none of the variables in mtcars are constant within carb. Further, if we have a lot of IDs (I have >50,000 in a current example), the above approach will be much harder to interpret directly.

Sticking with mtcars, we can see that there are a few variables that are constant within disp:

data.table(mtcars)[,lapply(.SD,uniqueN),by=disp
][,!"disp"][,lapply(.SD,table)]

mpg cyl hp drat wt qsec vs am gear carb
1: 24 27 26 26 24 23 27 27 27 26
2: 2 27 1 1 2 3 27 27 27 1
3: 1 27 26 26 1 1 27 27 27 26

Hence, vs, am, and gear are constant within disp.

Extracting columns with constant numbers in R data.frames

We can first find constant columns and then use lapply to loop over them and select only their first row in each study.name.

is_constant <- function(x) length(unique(x)) == 1L 
cols <- names(Filter(all, aggregate(.~study.name, DATA, is_constant)[-1]))

L[cols] <- lapply(L[cols], function(x)
x[ave(x[[1]], DATA$study.name, FUN = seq_along) == 1, ])
L

#$ESL
# ESL ESL.1
#1 1 1
#7 2 2
#9 1 1
#17 1 1
#23 1 1
#35 1 1
#37 2 2
#49 2 2

#$prof
# prof prof.1
#1 2 2
#7 2 2
#9 3 3
#17 2 2
#23 2 2
#35 2 2
#37 NA NA
#49 2 2
#.....


Related Topics



Leave a reply



Submit