Using RHow can i use data files to store multidimensional arrays?
Page 1 of 1
14 Replies - 784 Views - Last Post: 11 December 2009 - 05:46 AM
#1
Using R
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!
thanks!
Replies To: Using R
#3
#4
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.
#5
Re: Using R
Posted 06 December 2009 - 12:25 PM
KYA, 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!
#6
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
#7
Re: Using R
Posted 06 December 2009 - 01:09 PM
KYA, 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".
#8
Re: Using R
Posted 06 December 2009 - 01:11 PM
Ok, so how does 'R' expect the file to be formatted?
#9
Re: Using R
Posted 06 December 2009 - 01:27 PM
KYA, 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...
#10
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...
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...
#11
Re: Using R
Posted 06 December 2009 - 02:23 PM
KYA, 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...
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...
#12
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?
#13
Re: Using R
Posted 11 December 2009 - 05:28 AM
KYA, 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!!
#14
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.
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).
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).
#15
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?
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|