Questions tagged [assign]

Something related to an assignment operation, i.e. the process of changing the content of a variable to reflect some given value.

1695 questions
270
votes
16 answers

How to assign from a function which returns more than one value?

Still trying to get into the R logic... what is the "best" way to unpack (on LHS) the results from a function returning multiple values? I can't do this apparently: R> functionReturningTwoValues <- function() { return(c(1, 2)) } R>…
mariotomo
  • 9,438
  • 8
  • 47
  • 66
113
votes
15 answers

Assign multiple new variables on LHS in a single line

I want to assign multiple variables in a single line in R. Is it possible to do something like this? values # initialize some vector of values (a, b) = values[c(2,4)] # assign a and b to values at 2 and 4 indices of 'values' Typically I want to…
user236215
  • 7,278
  • 23
  • 59
  • 87
109
votes
6 answers

How to name variables on the fly?

Is it possible to create new variable names on the fly? I'd like to read data frames from a list into new variables with numbers at the end. Something like orca1, orca2, orca3... If I try something like paste("orca",i,sep="")=list_name[[i]] I get…
Maiasaura
  • 32,226
  • 27
  • 104
  • 108
59
votes
4 answers

setq and defvar in Lisp

I see that the Practical Common Lisp uses (defvar *db* nil) for setting up a global variable. Isn't it OK to use setq for the same purpose? What are the advantages/disadvantages of using defvar vs. setq?
prosseek
  • 182,215
  • 215
  • 566
  • 871
48
votes
4 answers

Initializing a vector of vectors having a fixed size with boost assign

Having a vector of vector with a fixed size, vector > v(10); I would like to initialize it so that it has in all elements a one dimensional vector with initialized value (for example 1). I have used Boost Assign as follows v =…
saloua
  • 2,433
  • 4
  • 27
  • 37
41
votes
3 answers

Why is using assign bad?

This post (Lazy evaluation in R – is assign affected?) covers some common ground but I am not sure it answers my question. I stopped using assign when I discovered the apply family quite a while back, albeit, purely for reasons of elegance in…
asb
  • 4,392
  • 1
  • 20
  • 30
39
votes
4 answers

R: Assign variable labels of data frame columns

I am struggling with variable labels of data.frame columns. Say I have the following data frame (part of much larger data frame): data <- data.frame(age = c(21, 30, 25, 41, 29, 33), sex = factor(c(1, 2, 1, 2, 1, 2), labels = c("Female",…
panman
  • 1,179
  • 1
  • 13
  • 33
37
votes
2 answers

R: what's the proper way to overwrite a function from a package?

I am using a R package, in which there are 2 functions f1 and f2 (with f2 calling f1) I wish to overwrite function f1. Since R 2.15 and the mandatory usage of namespace in packages, if I just source the new function, it is indeed available in the…
RockScience
  • 17,932
  • 26
  • 89
  • 125
35
votes
4 answers

Assign multiple objects to .GlobalEnv from within a function

A post on here a day back has me wondering how to assign values to multiple objects in the global environment from within a function. This is my attempt using lapply (assign may be safer than <<- but I have never actually used it and am not…
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
31
votes
4 answers

In R, how to make the variables inside a function available to the lower level function inside this function?(with, attach, environment)

Update 2 @G. Grothendieck posted two approaches. The second one is changing the function environment inside a function. This solves my problem of too many coding replicates. I am not sure if this is a good method to pass through the CRAN check when…
Zhenglei
  • 988
  • 1
  • 10
  • 21
27
votes
4 answers

How to make object created within function usable outside

I created a function which produces a matrix as a result, but I can't figure out how to make the output of this function usable outside of the function environment, so that I could for instance save it in csv file. My code for function is the…
26
votes
1 answer

Why can I assign structs but not compare them

Even though I am a long time C programmer, I only recently learned that one can directly assign structure variables to one another instead of using memcpy: struct MyStruct a,b; ... a = b; /* implicit memcpy */ Though this feels a bit "high-level"…
Tomas
  • 5,067
  • 1
  • 35
  • 39
25
votes
3 answers

`x = y, z` comma assignment in JavaScript

Possible Duplicate: Javascript syntax: what comma means? I came across the code while reading this article (do a Ctrl+F search for Andre Breton): //function returning array of `umbrella` fibonacci numbers function Colette(umbrella) { var…
24
votes
4 answers

Assigning vs. Defining Python Magic Methods

Consider the following abhorrent class: class MapInt: __call__ = int def __sub__(self, other): return map(self, other) __add__ = map One can then call map(int, lst) via MapInt() - lst, i.e. assert list(MapInt() -…
kg583
  • 408
  • 2
  • 8
24
votes
1 answer

Pandas how to pass DataFrame.assign arguments to add multiple new columns?

How can assign be used to return a copy of the original DataFrame with multiple new columns added? Desired result: df = pd.DataFrame({'A': range(1, 5), 'B': range(11, 15)}) >>> df.assign({'C': df.A.apply(lambda x: x ** 2), 'D': df.B * 2}) A B …
Alexander
  • 105,104
  • 32
  • 201
  • 196
1
2 3
99 100