Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the functional form of the assignment operator, [<-?

Tags:

r

Is there a functional form of the assignment operator? I would like to be able to call assignment with lapply, and if that's a bad idea I'm curious anyways.

Edit:

This is a toy example, and obviously there are better ways to go about doing this:

Let's say I have a list of data.frames, dat, each corresponding to a one run of an experiment. I would like to be able to add a new column, "subject", and give it a sham-name. The way I was thinking of it was something like

lapply(1:3, function(x) assign(data.frame = dat[[x]], column="subject", value=x)

The output could either be a list of modified data frames, or the modification could be purely a side effect.

dput of list starting list

list(structure(list(V1 = c(-1.16664504687199, -0.429499924318301,  2.15470735901367, -0.287839633854442, -0.850578353982526, 0.211636723222015,  -0.184714165752958, -0.773553182015158, 0.801811848828454, 1.39420292299319 ), V2 = c(-0.00828185523886259, -0.0215669898046275, 0.743065397283645,  -0.0268464140141802, 0.168027242784788, -0.602901928341917, 0.0740511186398372,  0.180307494696194, 0.131160421341309, -0.924995634374182)), .Names = c("V1",  "V2"), row.names = c(NA, -10L), class = "data.frame"), structure(list(     V1 = c(1.81912921386885, 1.17011641727415, 0.692247839769473,      0.0323050362633069, 1.35816977313292, -0.437475434344363,      -0.270255715332778, 0.96140963297774, 0.914691132220417,      -1.8014509598977), V2 = c(1.45082316226241, 2.05135744606495,      -0.787250759618171, 0.288104852581324, -0.376868533959846,      0.531872044490353, -0.750375220117567, -0.459592764008714,      0.991667163481123, 1.31280356980115)), .Names = c("V1", "V2" ), row.names = c(NA, -10L), class = "data.frame"), structure(list(     V1 = c(0.528912899341174, 0.464615157920766, -0.184211714281637,      0.526909095449027, -0.371529800682086, -0.483772861751781,      -2.02134822661341, -1.30841566046747, -0.738493559993166,      -0.221463545903242), V2 = c(-1.44732101816006, -0.161730785376045,      1.06294520132753, 1.22680614207705, -0.721565979363022, -0.438309438404104,      -0.0243401435910825, 0.624227513999603, 0.276605218579759,      -0.965640602482051)), .Names = c("V1", "V2"), row.names = c(NA,  -10L), class = "data.frame"))
like image 445
Nathan Avatar asked Jul 13 '15 20:07

Nathan


2 Answers

Maybe I don't get it but as stated in "The Art of R programming":

Any assignment statement in which the left side is not just an identifier (meaning a variable name) is considered a replacement function.

and so in fact you can always translate this:

names(x) <- c("a","b","ab")

to this:

x <- "names<-"(x,value=c("a","b","ab"))

the general rule is just "function_name<-"(<object>, value = c(...))

Edit to the comment:

It works with the " too:

> x <- c(1:3)
> x
[1] 1 2 3
> names(x) <- c("a","b","ab")
> x
 a  b ab 
 1  2  3 
> x
 a  b ab 
 1  2  3 
> x <- c(1:3)
> x
[1] 1 2 3
> x <- "names<-"(x,value=c("a","b","ab"))
> x
 a  b ab 
 1  2  3 
like image 159
SabDeM Avatar answered Sep 30 '22 21:09

SabDeM


There is the assign function. I don't see any problems with using it but you have to be aware of what environment you want to assign to. See the help ?assign for syntax.

Read this chapter carefully to understand the ins and outs of environments in detail. http://adv-r.had.co.nz/Environments.html

like image 26
Mike Wise Avatar answered Sep 30 '22 20:09

Mike Wise