Loading matrix of string and numbers

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

Loading matrix of string and numbers

AlbFrigerio
Hello world, I have a question about loading commands. I tried to look for the answer in the forum but I didn't find it ; I believe someone already made this question, but I cannot find the post :(

By the way, the question is quite simple: is it possible to load a matrix A which consts of both string (first column) and numbers (second one), so that A(i,1) returns a string and A(i,2) a number? See the attached file for a simple (stupid) example.

test.txt

I tried to use [a,b]=textread("test.txt",'%s %d') , but I got a cell array (a) and a vector (b) and, even it is a acceptable solution, it is not the one I'd like: if I got a lot of columsn I should use [a,b,c,d, ... , goofy, mickey, donald , ... , antares, betelgeus, aldebaran, ...] = textread("test.txt",'%s %d %s ... ') , and it is not smart :)

Thanks everyone, hace a nice day!!

   Alberto
Reply | Threaded
Open this post in threaded view
|

Re: Loading matrix of string and numbers

CdeMills
Hello,

I developped a package called 'dataframe' that you can download from octave.sourceforge.net. Its purpose is to implement something similar to R data.frame, i.e. a generic storage mechanism, including row and column information, where each column is a basic octave type: char / int / double / ... It's a kind of 'cell' on steroids, and the class constructor may be passed as argument the name of the file, from which it tries hard to infer the column content.

Regards

Pascal
Reply | Threaded
Open this post in threaded view
|

Re: Loading matrix of string and numbers

AlbFrigerio
Thank you so much, it sounds very interesting, but I got some problems in istalling it :(

Following the instructions in the site, I downloaded your package and put in the bin folder on my PC. Then I typed pkg install dataframe-0.8.2.tar.gz on my prompt, but then typing help dataframe I got the error saying that function doesn't exist :(

By the way, I'll try to solve this problem. Afterwise, should I only type dataframe("test.txt") ?

Thanks againg,
   Alberto


CdeMills wrote
Hello,

I developped a package called 'dataframe' that you can download from octave.sourceforge.net. Its purpose is to implement something similar to R data.frame, i.e. a generic storage mechanism, including row and column information, where each column is a basic octave type: char / int / double / ... It's a kind of 'cell' on steroids, and the class constructor may be passed as argument the name of the file, from which it tries hard to infer the column content.

Regards

Pascal
Reply | Threaded
Open this post in threaded view
|

Re: Loading matrix of string and numbers

CdeMills
AlbFrigerio wrote
Thank you so much, it sounds very interesting, but I got some problems in istalling it :(

Following the instructions in the site, I downloaded your package and put in the bin folder on my PC. Then I typed pkg install dataframe-0.8.2.tar.gz on my prompt, but then typing help dataframe I got the error saying that function doesn't exist :(

By the way, I'll try to solve this problem. Afterwise, should I only type dataframe("test.txt") ?
In theory, yes. Maybe type

pkg load 'dataframe'
to activate it ?

Then,
x=dataframe('test.txt') should return a dataframe object. Afterwards, you can extract parts, assign new values, and much more.

Regards

Pascal
Reply | Threaded
Open this post in threaded view
|

Re: Loading matrix of string and numbers

forkandwait
In reply to this post by AlbFrigerio
AlbFrigerio <alberto.frigerio1 <at> studenti.unimi.it> writes:

> By the way, the question is quite simple: is it possible to load a matrix A
> which consts of both string (first column) and numbers (second one), so that
> A(i,1) returns a string and A(i,2) a number? See the attached file for a
> simple (stupid) example.

It actually isn't that simple in Octave/ML, because regularly matrices are
numeric only (or character only, but you don't want to go there).  If you want
mixed types in the same matrix, you can only use cell arrays, which are limited
in that you can't use most things like sum, etc.

If possible, you might recode your data outside of octave to give integers to
all the string values.  Octave and its proprietary inspiration aren't very good
at handling categorical or string data, at least not yet ....

> I tried to use [a,b]=textread("test.txt",'%s %d') , but I got a cell array
> (a) and a vector (b) and, even it is a acceptable solution, it is not the
> one I'd like: if I got a lot of columsn I should use [a,b,c,d, ... , goofy,
> mickey, donald , ... , antares, betelgeus, aldebaran, ...] =
> textread("test.txt",'%s %d %s ... ') , and it is not smart :)

That is probably the best solution except for dataframe, which is experimental
but very promising.

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Loading matrix of string and numbers

AlbFrigerio
In reply to this post by CdeMills
CdeMills wrote
AlbFrigerio wrote
Thank you so much, it sounds very interesting, but I got some problems in istalling it :(

Following the instructions in the site, I downloaded your package and put in the bin folder on my PC. Then I typed pkg install dataframe-0.8.2.tar.gz on my prompt, but then typing help dataframe I got the error saying that function doesn't exist :(

By the way, I'll try to solve this problem. Afterwise, should I only type dataframe("test.txt") ?
In theory, yes. Maybe type

pkg load 'dataframe'
to activate it ?

Then,
x=dataframe('test.txt') should return a dataframe object. Afterwards, you can extract parts, assign new values, and much more.

Regards

Pascal

Thank  you so mych, you were completely right. The only "problem" is that every time I launch on Octave session I have to type your commands, but I will easily remember it!! Thanks again for your suggest, this package sounds GREAT !!!
Reply | Threaded
Open this post in threaded view
|

Re: Loading matrix of string and numbers

Jordi Gutiérrez Hermoso
On 16 February 2011 14:48, AlbFrigerio
<[hidden email]> wrote:
> Thank  you so mych, you were completely right. The only "problem" is that
> every time I launch on Octave session I have to type your commands, but I
> will easily remember it!!

If you need commands to run at load time, you can put them in your
.octaverc initialisation file. I didn't understand what operating
system you're using, so I don't know exactly where your .octaverc nor
why the package isn't adding the pkg load command to the system-wide
initialisation files.

At any rate, HTH,
- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Loading matrix of string and numbers

AlbFrigerio
Jordi Gutiérrez Hermoso wrote
On 16 February 2011 14:48, AlbFrigerio
<alberto.frigerio1@studenti.unimi.it> wrote:
> Thank  you so mych, you were completely right. The only "problem" is that
> every time I launch on Octave session I have to type your commands, but I
> will easily remember it!!

If you need commands to run at load time, you can put them in your
.octaverc initialisation file. I didn't understand what operating
system you're using, so I don't know exactly where your .octaverc nor
why the package isn't adding the pkg load command to the system-wide
initialisation files.

At any rate, HTH,
- Jordi G. H.
_______________________________________________
Help-octave mailing list
Help-octave@octave.org
https://mailman.cae.wisc.edu/listinfo/help-octave
Thank you so much, this will be a great help. Currently, I'm running Octave 3.2.4 on Windows XP (32) or Windows 7 (64) , depending on the PC I find without user ;)

Bye,
  Alberto