[R] Getting names of variables without quotes

Amarjit Singh Sethi set_alt at yahoo.co.in
Sun Oct 19 20:31:14 CEST 2008


Dear Jorge

Many thanks for your prompt reply. I tried to make use of the code provided by you (as per file attached), but it did not work for me; got the following output:

> # -------------------
> # Data set
> # -------------------
> mydata=read.table(textConnection(" 
+   V1V2V3
+ 115104
+ 2647
+ 31052
+ 4866"),header=TRUE)
> closeAllConnections()
> # -------------------
> # Regression models
> # -------------------
> # Combinations
> library(forward)
> comb=t(fwd.combn(colnames(mydata),2))
Error in fwd.combn(colnames(mydata), 2) : n < m
> # Summariesres=apply(comb,1,function(x){
> y1=mydata[,x[1]]
Error in .subset(x, j) : invalid subscript type 'list'
> x1=mydata[,x[2]]
Error in .subset(x, j) : invalid subscript type 'list'
> m=lm(y1~x1)
Error in eval(expr, envir, enclos) : object "y1" not found
> summary(m)
Error in summary(m) : object "m" not found
> }
Error: unexpected '}' in "}"
> )
Error: unexpected ')' in ")"
> names(res)= apply(comb,1,paste,collapse="/",sep=" ")
Error in apply(comb, 1, paste, collapse = "/", sep = " ") : 
  object "comb" not found
> # Output in an external file
> sink("E:/out.txt")
> res
Error: object "res" not found
> sink()
>

I do not know as to where the problem lies (Frankly, I have not been able to grasp intricacies of this code). Will you kindly help me in getting rid of the problem.

Regards
ajss


--- On Sun, 19/10/08, Jorge Ivan Velez <jorgeivanvelez at gmail.com> wrote:

From: Jorge Ivan Velez <jorgeivanvelez at gmail.com>
Subject: Re: [R] Getting names of variables without quotes
To: set_alt at yahoo.co.in
Cc: "Dieter Menne" <dieter.menne at menne-biomed.de>, "R mailing list" <r-help at r-project.org>
Date: Sunday, 19 October, 2008, 9:19 PM




Dear AJSS,


Perhaps:



# -------------------
# Data set
# -------------------


mydata=read.table(textConnection("
   V1    V2    V3
1  15     10      4
2    6      4       7
3  10      5       2
4    8      6       6"),header=TRUE)
closeAllConnections()


# -------------------
# Regression models
# -------------------


# Combinations
library(forward)
comb=t(fwd.combn(colnames(mydata),2))


# Summaries
res=apply(comb,1,function(x){
y1=mydata[,x[1]]
x1=mydata[,x[2]]
m=lm(y1~x1)
summary(m)
}
)
names(res)= apply(comb,1,paste,collapse="/",sep="")


# Output in an external file
sink("C:/out.txt")
res
sink()


HTH,


Jorge




On Sun, Oct 19, 2008 at 11:38 AM, Amarjit Singh Sethi <set_alt at yahoo.co.in> wrote:






Dear Jorge/Dieter
Thank you very much for your help. I indeed have been able to get names of the variables through 'noquote( )'statement. Yet my problem (regarding running regression analysis iteratively: of V1 upon V2; of V1 upon V3; and of V2 upon V3) could not be solved. My input data:


    V1    V2    V3
1  15     10      4
2    6      4       7
3  10      5       2
4    8      6       6

I tried the following code:

x=read.table("sample.txt",header=T,sep="\t")
sink("out.txt")
for (i in 1:2){
dep=noquote(nm[i])
for(j in i+1:3){
ind=noquote(nm[j])
slr=lm(dep ~ ind, data=x)
smr=summary(slr)
smr
}
}
sink()
But I could not get any results. Kindly help or suggest suitable alternative.
The data and the code are given as attachments also.
Regards
ajss
 

--- On Sun, 19/10/08, Jorge Ivan Velez <jorgeivanvelez at gmail.com> wrote:

From: Jorge Ivan Velez <jorgeivanvelez at gmail.com>
Subject: Re: [R] Getting names of variables without quotes
To: set_alt at yahoo.co.in
Cc: r-help at r-project.org
Date: Sunday, 19 October, 2008, 1:04 AM








Dear AJSS, 


Perhaps ?noquote might be useful for you. Here is an example:


> x=c('V1','V2','V3','V4')


> x
[1] "V1" "V2" "V3" "V4"
> noquote(x)
[1] V1 V2 V3 V4

HTH,


Jorge





On Sat, Oct 18, 2008 at 3:04 PM, Amarjit Singh Sethi <set_alt at yahoo.co.in> wrote:

 
Dear R-helpers,
hello
I am seeking your help in somehow getting names of variables without quotes (" ").
Let us say, we have a table with 3 variables V1, V2 and V3. After the table is read, I get names of the variables (thro' the following code) as under quotes (like "V1" rather than the original representation V1)
 
 x=read.table("sample.txt",header=T,sep="\t")
> x
    V1    V2    V3
1  15     10      4
2    6      4       7
3  10      5       2
4    8      6       6
> nm=names(x)
> nm
[1] "V1" "V2" "V3"
 
In fact I need the variables in the original representation (i.e.., as they appear in the input data file) so as to use them repeatedly (through loop statement) in regression analysis. Kindly help.
Regards
ajss




d Now! http://messenger.yahoo.com/download.php
       [[alternative HTML version deleted]]


______________________________________________
R-help at 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.






-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: Regression Model through help.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20081020/6de07461/attachment.txt>


More information about the R-help mailing list