Convert list to data frame

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

Convert list to data frame

asma.rabe
Hi,

I  read data from file as follows

Data<-read.table("file.txt",header=T,sep="\t")

mode(Data)
list

I want to convert data to data frame, I tried the following:

as.data.frame(Data)
data.frame(Data)

But the Data did not change

When I tried
as.data.frame(unlist(Data))

The Data converted to a vector not to a data frame. Any idea ?

Thank you in advance


Sent from my iPhone

______________________________________________
[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: Convert list to data frame

Ivan Calandra-4
Hi,

I have seen this question a few days/weeks ago...

Data.frames are special list, so it's normal.
Read the help for read.table(), especially the "value" section (where
the output of the function is described). And read also some
introductory material, where the different data types are explained.

HTH,
Ivan

--
Ivan Calandra, PhD
University of Reims Champagne-Ardenne
GEGENAA - EA 3795
CREA - 2 esplanade Roland Garros
51100 Reims, France
+33(0)3 26 77 36 89
[hidden email]
--
https://www.researchgate.net/profile/Ivan_Calandra
https://publons.com/author/705639/

Le 27/02/2016 15:04, [hidden email] a écrit :

> Hi,
>
> I  read data from file as follows
>
> Data<-read.table("file.txt",header=T,sep="\t")
>
> mode(Data)
> list
>
> I want to convert data to data frame, I tried the following:
>
> as.data.frame(Data)
> data.frame(Data)
>
> But the Data did not change
>
> When I tried
> as.data.frame(unlist(Data))
>
> The Data converted to a vector not to a data frame. Any idea ?
>
> Thank you in advance
>
>
> Sent from my iPhone
>
> ______________________________________________
> [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: Convert list to data frame

Karim Mezhoud
To known the format of your object, please use
class(Data)
str(Data)

Be sure to have regular space between strings in your file.txt.
Karim

On Sat, Feb 27, 2016 at 3:56 PM, Ivan Calandra <[hidden email]>
wrote:

> Hi,
>
> I have seen this question a few days/weeks ago...
>
> Data.frames are special list, so it's normal.
> Read the help for read.table(), especially the "value" section (where the
> output of the function is described). And read also some introductory
> material, where the different data types are explained.
>
> HTH,
> Ivan
>
> --
> Ivan Calandra, PhD
> University of Reims Champagne-Ardenne
> GEGENAA - EA 3795
> CREA - 2 esplanade Roland Garros
> 51100 Reims, France
> +33(0)3 26 77 36 89
> [hidden email]
> --
> https://www.researchgate.net/profile/Ivan_Calandra
> https://publons.com/author/705639/
>
>
> Le 27/02/2016 15:04, [hidden email] a écrit :
>
>> Hi,
>>
>> I  read data from file as follows
>>
>> Data<-read.table("file.txt",header=T,sep="\t")
>>
>> mode(Data)
>> list
>>
>> I want to convert data to data frame, I tried the following:
>>
>> as.data.frame(Data)
>> data.frame(Data)
>>
>> But the Data did not change
>>
>> When I tried
>> as.data.frame(unlist(Data))
>>
>> The Data converted to a vector not to a data frame. Any idea ?
>>
>> Thank you in advance
>>
>>
>> Sent from my iPhone
>>
>> ______________________________________________
>> [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.
>

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

Re: Convert list to data frame

David Winsemius
In reply to this post by asma.rabe

> On Feb 27, 2016, at 6:04 AM, <[hidden email]> <[hidden email]> wrote:
>
> Hi,
>
> I  read data from file as follows
>
> Data<-read.table("file.txt",header=T,sep="\t")
>
> mode(Data)
> list
>
> I want to convert data to data frame,

It is already a dataframe. That is the class of object that read.table returns.


> I tried the following:
>
> as.data.frame(Data)
> data.frame(Data)
>
> But the Data did not change

R is a functional language. Simply applying a function does NOT alter the value of the arguments. Need to use assignment. If Data had not been a dataframe already and it had been a list with values whose lengths were equal, then you would have needed to perform:

Data <- data.frame(Data)


>
> When I tried
> as.data.frame(unlist(Data))
>
> The Data converted to a vector not to a data frame. Any idea ?
>
>


David Winsemius
Alameda, CA, USA

______________________________________________
[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: Convert list to data frame

Jeff Newmiller
In reply to this post by Karim Mezhoud
I think the advice about the file format is an track, but you imply modifying the file as a solution but that is probably not the best approach. Using a decent text editor that shows you what invisible characters are in the file can guide you in adjusting the arguments to read.table. for example,  the OP specified tab separators between fields, as Excel might put between fields in a text file. However, that is not a universal standard for text files so if spaces are used then removing the sep argument from the read.table call might help.
--
Sent from my phone. Please excuse my brevity.

On February 27, 2016 7:52:06 AM PST, Karim Mezhoud <[hidden email]> wrote:

>To known the format of your object, please use
>class(Data)
>str(Data)
>
>Be sure to have regular space between strings in your file.txt.
>Karim
>
>On Sat, Feb 27, 2016 at 3:56 PM, Ivan Calandra
><[hidden email]>
>wrote:
>
>> Hi,
>>
>> I have seen this question a few days/weeks ago...
>>
>> Data.frames are special list, so it's normal.
>> Read the help for read.table(), especially the "value" section (where
>the
>> output of the function is described). And read also some introductory
>> material, where the different data types are explained.
>>
>> HTH,
>> Ivan
>>
>> --
>> Ivan Calandra, PhD
>> University of Reims Champagne-Ardenne
>> GEGENAA - EA 3795
>> CREA - 2 esplanade Roland Garros
>> 51100 Reims, France
>> +33(0)3 26 77 36 89
>> [hidden email]
>> --
>> https://www.researchgate.net/profile/Ivan_Calandra
>> https://publons.com/author/705639/
>>
>>
>> Le 27/02/2016 15:04, [hidden email] a écrit :
>>
>>> Hi,
>>>
>>> I  read data from file as follows
>>>
>>> Data<-read.table("file.txt",header=T,sep="\t")
>>>
>>> mode(Data)
>>> list
>>>
>>> I want to convert data to data frame, I tried the following:
>>>
>>> as.data.frame(Data)
>>> data.frame(Data)
>>>
>>> But the Data did not change
>>>
>>> When I tried
>>> as.data.frame(unlist(Data))
>>>
>>> The Data converted to a vector not to a data frame. Any idea ?
>>>
>>> Thank you in advance
>>>
>>>
>>> Sent from my iPhone
>>>
>>> ______________________________________________
>>> [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.
>>
>
> [[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.

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