> On Oct 13, 2017, at 6:50 AM, y tanaka <marineband2...@gmail.com> wrote: > > Dear mailing list members, > > My question is maybe very basic, but I could not find the solution. > > I would like to do the following things > > 1) > colnames(V1)[2] <- par$V2[1] > colnames(V2)[2] <- par$V2[2] > colnames(V3)[2] <- par$V2[3] > ... > colnames(V37)[2] <- par$V2[37]
You are making a basic error in planning your analysis. The items V1-V37 should be in a list. Then you just do something like this, bigVlist <- mapply( function(x,y) {colnames(x)[2] <- y}, bigVlist, par$V2) > > 2) > V1 <- V1[,-1] > V2 <- V2[,-1] > V3 <- V3[,-1] > ... > V37 <- V37[,-1] bigVlist <- lapply( bigVlist , function(x) x[ , -1]) > > 3) > ms <- merge(V1,V2) > ms <- merge(ms,V3) > ms <- merge(ms,V4) > ... > ms <- merge(ms,V37) ms <- do.call(merge, bigVlist) If you had provided a sample of items with equivalent structure to your actual use case we might have been motivated to provide tested code. > > Since these codes take a lot of space in my R console and I am also afraid > of making any mistakes while typing the codes, I would like to make this > code simple by using for loop. > > I know that the assign function is useful and another code as follows > worked well with it: > for(i in 1:N_var){ > assign(paste("V", i, sep=""), post_df[Nrange$start[i]:Nrange$end[i],]) > } > > However, I could not find how to apply for loop to 1) - 3) above. > > 1) >> for (i in 1:N_var){ > + assign(colnames(paste("V", i, sep=""))[2], par$V2[i]) You cannot embed extraction/indexing functions on the "lefthand side", i.e. the first argument, of an `assign(` operation. -- David. > + } > Error in assign(colnames(paste("V", i, sep = ""))[2], par$V2[i]) : > invalid first argument > > 2) >> for(i in 1:N_var){ > assign(paste("V", i, sep=""), paste("V", i, sep="")[,-1]) > } > Error in paste("V", i, sep = "")[, -1] : incorrect number of dimensions > > > I suppose that it might be important to call object names without quotation > mark, so > I tried the noquote function, but it did not change the situation. > > 1) >> for (i in 1:N_var){ > + assign(colnames(noquote(paste("V", i, sep=""))[2], par$V2[i])) > + } > Error in if (do.NULL) NULL else if (nc > 0L) paste0(prefix, seq_len(nc)) > else character() : > argument is not interpretable as logical > > 2) >> for(i in 1:N_var){ > + assign(paste("V", i, sep=""), noquote(paste("V", i, sep=""))[,-1]) > + } > Error in unclass(x)[...] : incorrect number of dimensions > > I would appreciate it if someone would help me. > > Best regards, > Yohei Tanaka > ========================== > Yohei Tanaka > Tohoku University > Graduate school of Economics > Doctoral student > email: marineband2...@gmail.com > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. David Winsemius Alameda, CA, USA 'Any technology distinguishable from magic is insufficiently advanced.' -Gehm's Corollary to Clarke's Third Law ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.