r - What is the functional form of the assignment operator, [<-? -
is there functional form of assignment operator? able call assignment lapply, , if that's bad idea i'm curious anyways.
edit:
this toy example, , there better ways go doing this:
let's have list of data.frames, dat, each corresponding 1 run of experiment. able add new column, "subject", , give sham-name. way thinking of like
lapply(1:3, function(x) assign(data.frame = dat[[x]], column="subject", value=x) the output either list of modified data frames, or modification purely 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"))
maybe don't stated in "the art of r programming":
any assignment statement in left side not identifier (meaning variable name) considered replacement function.
and in fact can translate this:
names(x) <- c("a","b","ab") to this:
x <- "names<-"(x,value=c("a","b","ab")) the general rule "function_name<-"(<object>, value = c(...))
edit comment:
it works " too:
> x <- c(1:3) > x [1] 1 2 3 > names(x) <- c("a","b","ab") > x b ab 1 2 3 > x b ab 1 2 3 > x <- c(1:3) > x [1] 1 2 3 > x <- "names<-"(x,value=c("a","b","ab")) > x b ab 1 2 3
Comments
Post a Comment