Hi All: I'm looking to find out a bit more about how subassignment actually works and am hoping someone with knowledge of the details can fill me in (I've looked at the source code, but my knowledge of C is lacking). In the case of vectors, my reading of ?"[" would indicate that for a vector, vec <- 1:25, vec[c(1,5,25)] <- c(101,102,103)is functionally the same as indx <- c(1,5,25) for (i in 1:length(indx)) vec[indx[i]] <- c(101,102,103)[i] And in the case of a data frame, df <- data.frame(d1=1:10,d2=11:20,d3=21:30), df[c(1,5,10),c(1,3)] <- data.frame(a=101:103,b=104:106)is functionally the same as rowindx <- c(1,5,10) colindx <- c(1,3) for (i in 1:length(rowindx)) { for (j in 1:length(colindx)) df[rowindx[i],colindx[j]] <- data.frame(a=101:103,b=104:106)[i,j] } Obviously I've verified that these examples work and I realize that my loops also contain subassignments; what I'm really after is to understand the mechanics of replacing multiple elements. Is a for-loop the proper way to understand the sequential nature of subassignments here (even if it is not actually implemented using a loop)? Cheers,HR [[alternative HTML version deleted]]
Jeff Newmiller
2011-Aug-12 06:06 UTC
[R] Details of subassignment (for vectors and data frames)
My mental model is that the left hand side forms a sort of "virtual vector" where each element really points to an element in the vector being modified. Then the right hand side scalar is extended in the usual repetitious way (if necessary) until it is a vector just as long as the "virtual vector" on the left. Then the right vector is assigned to the left vector, which leaves the designated elements in the destination vector changed. You could work out an equivalent for loop structure, but I think it would be tricky to keep the behavior for different length source and destination assignments straight. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Al Roark <hrbuilder@hotmail.com> wrote: Hi All: I'm looking to find out a bit more about how subassignment actually works and am hoping someone with knowledge of the details can fill me in (I've looked at the source code, but my knowledge of C is lacking). In the case of vectors, my reading of ?"[" would indicate that for a vector, vec <- 1:25, vec[c(1,5,25)] <- c(101,102,103)is functionally the same as indx <- c(1,5,25) for (i in 1:length(indx)) vec[indx[i]] <- c(101,102,103)[i] And in the case of a data frame, df <- data.frame(d1=1:10,d2=11:20,d3=21:30), df[c(1,5,10),c(1,3)] <- data.frame(a=101:103,b=104:106)is functionally the same as rowindx <- c(1,5,10) colindx <- c(1,3) for (i in 1:length(rowindx)) { for (j in 1:length(colindx)) df[rowindx[i],colindx[j]] <- data.frame(a=101:103,b=104:106)[i,j] } Obviously I've verified that these examples work and I realize that my loops also contain subassignments; what I'm really after is to understand the mechanics of replacing multiple elements. Is a for-loop the proper way to understand the sequential nature of subassignments here (even if it is not actually implemented using a loop)? Cheers,HR [[alternative HTML version deleted]] _____________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]