A list of data frames and a list of colnames.

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

A list of data frames and a list of colnames.

Ed Siefker
I have a list of file names, and a list of data frames contained in those files.

mynames <- list.files()
mydata <- lapply(mynames, read.delim)

Every file contains two columns.

> colnames(mydata[[1]])
[1] "Name"     "NumReads"
> colnames(mydata[[2]])
[1] "Name"     "NumReads"

I can set the colnames easily enough with a for loop.

for (i in seq_along(mynames)) {
    colnames(mydata[[i]])[2] <- mynames[i]
}

Is there a nicer way to do this?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: A list of data frames and a list of colnames.

Rui Barradas
Hello,

I think that your code is simple enough to be considered "nice". If you
are worried about the for loop, don't, were loops worrying they wouldn't
exist.

Hope this helps,

Rui Barradas

Em 23-10-2017 22:09, Ed Siefker escreveu:

> I have a list of file names, and a list of data frames contained in those files.
>
> mynames <- list.files()
> mydata <- lapply(mynames, read.delim)
>
> Every file contains two columns.
>
>> colnames(mydata[[1]])
> [1] "Name"     "NumReads"
>> colnames(mydata[[2]])
> [1] "Name"     "NumReads"
>
> I can set the colnames easily enough with a for loop.
>
> for (i in seq_along(mynames)) {
>      colnames(mydata[[i]])[2] <- mynames[i]
> }
>
> Is there a nicer way to do this?
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: A list of data frames and a list of colnames.

Bert Gunter-2
In reply to this post by Ed Siefker
This doesn't make sense to me:



On Mon, Oct 23, 2017 at 2:09 PM, Ed Siefker <[hidden email]> wrote:

> I have a list of file names, and a list of data frames contained in those
> files.
>
> mynames <- list.files()
>
## a character vector of file names

mydata <- lapply(mynames, read.delim)
>
# A list of data frames

>
> Every file contains two columns.
>
> > colnames(mydata[[1]])
> [1] "Name"     "NumReads"
>
# Note that names() can be used instead of colnames()

> > colnames(mydata[[2]])
> [1] "Name"     "NumReads"
>
# Ditto

>
> I can set the colnames easily enough with a for loop.
>
> for (i in seq_along(mynames)) {
>     colnames(mydata[[i]])[2] <- mynames[i] ## again, names() can be used
> instead of colnames
> }
>
> You are naming the  the 2nd column of the ith data frame with the file
name of the file from which the data frame was read. Is this really what
you want to do? Or have I misunderstood or erred?

Cheers,
Bert


Is there a nicer way to do this?
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.