Kenny Larsen
2009-Jun-15 15:50 UTC
[R] Assigning Data a name from within another variable?
Hi All,
I have hunted high and low and tried dozens of things but have yet to
achieve the result I require. Below is my code (taken mostly from another
thread on here) thus far:
files<-list.files()
files<-files[grep('.wm4', files)]
labels<-gsub('.wm4', '',files)
for(i in 1:length(files)){
X<-read.table(files[i])
<My problem is here!>
What I am trying to do now is assign the first item in labels in place of X
so the table name will effectively be whatever is written in labels[1]. The
files simply consist of two columns of real numbers. I have tried several
things, such like replacing X with labels[1], but this simply writes the
data in one long column to the first place of labels(which makes sense). How
do I extract what is in labels[1] and set it as the table name?
As always any help is greatly appreciated.
Kenny Larsen
--
View this message in context:
http://www.nabble.com/Assigning-Data-a-name-from-within-another-variable--tp24037279p24037279.html
Sent from the R help mailing list archive at Nabble.com.
Use a 'list' to read in the data and access it:
files<-list.files()
files<-files[grep('.wm4', files)]
labels<-gsub('.wm4', '',files)
myInput <- lapply(files, read.table)
# if you want to get rid of .wm4
names(myInput) <- sub(".wm4$", "", names(myInput))
On Mon, Jun 15, 2009 at 11:50 AM, Kenny Larsen
<k.larsen@sheffield.ac.uk>wrote:
>
> Hi All,
>
> I have hunted high and low and tried dozens of things but have yet to
> achieve the result I require. Below is my code (taken mostly from another
> thread on here) thus far:
>
> files<-list.files()
> files<-files[grep('.wm4', files)]
> labels<-gsub('.wm4', '',files)
>
> for(i in 1:length(files)){
> X<-read.table(files[i])
> <My problem is here!>
>
> What I am trying to do now is assign the first item in labels in place of X
> so the table name will effectively be whatever is written in labels[1]. The
> files simply consist of two columns of real numbers. I have tried several
> things, such like replacing X with labels[1], but this simply writes the
> data in one long column to the first place of labels(which makes sense).
> How
> do I extract what is in labels[1] and set it as the table name?
>
> As always any help is greatly appreciated.
>
> Kenny Larsen
> --
> View this message in context:
>
http://www.nabble.com/Assigning-Data-a-name-from-within-another-variable--tp24037279p24037279.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
[[alternative HTML version deleted]]
baptiste auguie
2009-Jun-15 16:34 UTC
[R] Assigning Data a name from within another variable?
Kenny Larsen wrote:> Hi All, > > I have hunted high and low and tried dozens of things but have yet to > achieve the result I require. Below is my code (taken mostly from another > thread on here) thus far: > > files<-list.files() > files<-files[grep('.wm4', files)] > labels<-gsub('.wm4', '',files) > > for(i in 1:length(files)){ > X<-read.table(files[i]) > <My problem is here!> > > What I am trying to do now is assign the first item in labels in place of X > so the table name will effectively be whatever is written in labels[1]. The > files simply consist of two columns of real numbers. I have tried several > things, such like replacing X with labels[1], but this simply writes the > data in one long column to the first place of labels(which makes sense). How > do I extract what is in labels[1] and set it as the table name? > > As always any help is greatly appreciated. > > Kenny Larsen >Hi, have you checked the r wiki, http://wiki.r-project.org/rwiki/doku.php?id=guides:assigning-variable-names this page is a really a work in progress, feel free to add comments. HTH, baptiste
Kenny Larsen
2009-Jun-16 13:23 UTC
[R] Assigning Data a name from within another variable?
Thnaks to both of you, with a combined effort of the Wiki and your code I have now managed to get teh result I was after. When I have a better understanding of what the code is doing I shall make an effort to contribut to the Wiki! Cheers, Kenny Kenny Larsen wrote:> > Hi All, > > I have hunted high and low and tried dozens of things but have yet to > achieve the result I require. Below is my code (taken mostly from another > thread on here) thus far: > > files<-list.files() > files<-files[grep('.wm4', files)] > labels<-gsub('.wm4', '',files) > > for(i in 1:length(files)){ > X<-read.table(files[i]) > <My problem is here!> > > What I am trying to do now is assign the first item in labels in place of > X so the table name will effectively be whatever is written in labels[1]. > The files simply consist of two columns of real numbers. I have tried > several things, such like replacing X with labels[1], but this simply > writes the data in one long column to the first place of labels(which > makes sense). How do I extract what is in labels[1] and set it as the > table name? > > As always any help is greatly appreciated. > > Kenny Larsen >-- View this message in context: http://www.nabble.com/Assigning-Data-a-name-from-within-another-variable--tp24037279p24054319.html Sent from the R help mailing list archive at Nabble.com.