[R] Quotation marks hinder for loop

y tanaka marineband2007 at gmail.com
Fri Oct 13 15:50:35 CEST 2017

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

colnames(V1)[2] <- par$V2[1]
colnames(V2)[2] <- par$V2[2]
colnames(V3)[2] <- par$V2[3]
colnames(V37)[2] <- par$V2[37]

V1 <- V1[,-1]
V2 <- V2[,-1]
V3 <- V3[,-1]
V37 <- V37[,-1]

ms <- merge(V1,V2)
ms <- merge(ms,V3)
ms <- merge(ms,V4)
ms <- merge(ms,V37)

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.

>for (i in 1:N_var){
+   assign(colnames(paste("V", i, sep=""))[2], par$V2[i])
+ }
Error in assign(colnames(paste("V", i, sep = ""))[2], par$V2[i]) :
  invalid first argument

>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.

> 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

> 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: marineband2007 �� gmail.com

	[[alternative HTML version deleted]]

More information about the R-help mailing list