Using R

How can i use data files to store multidimensional arrays?

Page 1 of 1

14 Replies - 998 Views - Last Post: 11 December 2009 - 05:46 AM Rate Topic: -----

#1 PhDfluke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-December 09

Using R

Post icon  Posted 06 December 2009 - 08:02 AM

Could someone point me in the right direction? I just need a tutorial or something about putting arrays into text files that can be read by "R"?

thanks!
Is This A Good Question/Topic? 0
  • +

Replies To: Using R

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: Using R

Posted 06 December 2009 - 08:04 AM

Is this the "R" of which you speak?
Was This Post Helpful? 0
  • +
  • -

#3 PhDfluke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-December 09

Re: Using R

Posted 06 December 2009 - 11:27 AM

View PostJackOfAllTrades, on 6 Dec, 2009 - 07:04 AM, said:




Yep!!! thats it!
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: Using R

Posted 06 December 2009 - 11:53 AM

Did you read the FAQ? It has info on using third party libraries, languages, and what not. If they have a forum, you are more likely to get a better answer there, especially since 'R' has nothing to do with C++ inherently.
Was This Post Helpful? 0
  • +
  • -

#5 PhDfluke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-December 09

Re: Using R

Posted 06 December 2009 - 12:25 PM

View PostKYA, on 6 Dec, 2009 - 10:53 AM, said:

Did you read the FAQ? It has info on using third party libraries, languages, and what not. If they have a forum, you are more likely to get a better answer there, especially since 'R' has nothing to do with C++ inherently.



Thanks!!

I have no idea how to convert my matrix that i have created into a datafile though...
I have just started learning c...i understand the process is the same as for c++...

this is the code i have written for it...
hprd=matrix(1,N,1,N); D=matrix(1,N,1,N);

for(i=1;i<=N;i++) for(j=1;j<=N;j++) {
if(i==j) D[i][j]=log (hprd[i][j])/log (gamma);
else D[i][j]=log (hprd[i][j])/log (gamma);

puts("Distance has been calculated successfully");

FILE *f;
f=fopen("Distance.mat","wb");
fwrite(&D,D[i][j],N,f);
fclose(f);

Would this mean that the matrix is printed or just that the first element is??
Would i be needing a for loop here?

thanks again!
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: Using R

Posted 06 December 2009 - 12:33 PM

So you want to import a datafile created by 'R' into a 2D array in C? we can do that. How is the data/text file formatted?

This post has been edited by KYA: 06 December 2009 - 01:11 PM

Was This Post Helpful? 0
  • +
  • -

#7 PhDfluke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-December 09

Re: Using R

Posted 06 December 2009 - 01:09 PM

View PostKYA, on 6 Dec, 2009 - 11:33 AM, said:

So you wat to import a datafile created by 'R' into a 2D array in C? we can do that. How is the data/text file formatted?



Actually i want to export to 'R'...so i want to create a simple text file (which i can use as an R script-named Distance) with each element of the array "D".
:D
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: Using R

Posted 06 December 2009 - 01:11 PM

Ok, so how does 'R' expect the file to be formatted?
Was This Post Helpful? 0
  • +
  • -

#9 PhDfluke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-December 09

Re: Using R

Posted 06 December 2009 - 01:27 PM

View PostKYA, on 6 Dec, 2009 - 12:11 PM, said:

Ok, so how does 'R' expect the file to be formatted?



basically i am gonna (hopefully) use my program to:

1) calcualte the matrix- as i have pretty much done
2) export the matrix to a file...either a datafile or a text file...
the text file format is ok...but if i can have a ".mat " file...containing the matrix..that would be amazing...

will the code i posted do this for me?

3) Send my supervisor the file...to format the file so it could be used in R...
Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: Using R

Posted 06 December 2009 - 02:17 PM

You can give a file any extension you want, it's only for the user anyway. datafile, textfile, all the same jazz.

What I meant was, how does R expect the file to be formatted?

i.e.

2x2 matrix:

data data data data

1 2 3 4

OR

data data
data data

1 2
3 4

OR

datadatadatadata

1234


etc...
Was This Post Helpful? 0
  • +
  • -

#11 PhDfluke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-December 09

Re: Using R

Posted 06 December 2009 - 02:23 PM

View PostKYA, on 6 Dec, 2009 - 01:17 PM, said:

You can give a file any extension you want, it's only for the user anyway. datafile, textfile, all the same jazz.

What I meant was, how does R expect the file to be formatted?

i.e.

2x2 matrix:

data data data data

1 2 3 4

OR

data data
data data

1 2
3 4

OR

datadatadatadata

1234


etc...



2D matrix...

i= 9400 or so proteins
j=9400 or so proteins





i.e. NxN matrix...
Was This Post Helpful? 0
  • +
  • -

#12 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: Using R

Posted 06 December 2009 - 02:46 PM

OK, last time I'm going to ask and then I'm done. How does 'R' want the data saved in a file. What type of structure?
Was This Post Helpful? 0
  • +
  • -

#13 PhDfluke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-December 09

Re: Using R

Posted 11 December 2009 - 05:28 AM

View PostKYA, on 6 Dec, 2009 - 01:46 PM, said:

OK, last time I'm going to ask and then I'm done. How does 'R' want the data saved in a file. What type of structure?



I went away and found out...

basically it should be

data data
data data

1 2
3 4

So i have

data data data data
data data data data
data data data data
data data data data

does this answerr your question?

Thanks again for your help!!
Was This Post Helpful? 0
  • +
  • -

#14 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Using R

Posted 11 December 2009 - 05:45 AM

Have I got this right?

You have a 2 dimensional array.
You want that output to a text file where each row of the matrix occupies a line in the text file and each element is separated by a single whitespace character.

If that's right then you can easily handle this with two nested for loops.
for( int row=0; row<ARRAY_ROWS; row++ ) {
	for( int col=0; col<ARRAY_COLS; col++ ) {
		//output the current element pointed to 
		// e.g. theArray[row][col]
		//output a whitespace character
		// e.g. ' '
	}
	// output an endline
	//e.g. '\n'
}



Make sense?

I'd suggest trying a small 2D array first and output to the screen.
Then output to a file.
Then output the full array to a file
Solid small steps make the progress faster in the long run (usually).
Was This Post Helpful? 0
  • +
  • -

#15 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Using R

Posted 11 December 2009 - 05:46 AM

R has a number of mechanisms for reading data. CSV, table, etc. and you can specify the separators, handling missing data, and so on. The point is, you need to decide the format precisely. If your columns are space separated, then you need to be careful about storing data (for example text) that itself contains spaces. You understand this right?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1