"For" loop than overwrites when using write.table - arrays

I am trying to extract every two consecutive columns from an array, writing a .dat with every pair. The problem is that, when using write.table() it overwrites the files. When I use print() instead of write.table() it shows the correct subsets, though.
I also need the file names to show the number of the pair of columns selected (a total of 6 pairs), as well as the number of the dimension (from 1 to 5). For this I have used an easier solution such as tagging 1:30.
for(i in 0:5) {
for (j in 1:5) {
for (k in 1:30) {
filename <- paste("Component",k, ".dat", sep="")
write.table(data[,c(2*i+1,2*i+2),j],col.names=F, row.names=F, sep= " ")
}
}
}
Any hints why it does not work?
I hope my goal is understandable. Thanks a lot for your time!

Set the argument append to TRUE:
write.table(data[,c(2*i+1,2*i+2),j],
file=filename,
append=TRUE,
col.names=F,
row.names=F,
sep= " ")
also, as correctly pointed out by #Roland, you have forgotten to pass the file argument (already added in my example above).

Related

2nd object in array ignored (godot)

I have a .csv file holding scores. This is what it looks like:
"name",10
The .csv is parsed with this function:
func _ready():
var file = File.new()
file.open("user://scores.csv", file.READ)
while !file.eof_reached():
line = file.get_csv_line()
text = text + "%s: %s" % [line[0], line[1]] + "\n" ## Invalid get index: '1' (on base: PoolStringArray)
The file must be ending in a new line. As a result, you get two lines while reading it. The first one has what you expect ([name, 10]), the second one is empty, causing an error when you try to index it.
When I tested this, the last line does not come as an empty array, but as an array with a single empty string. Looking around, this behavior has been there for a while. However, I found no bug reports or proposals, I wonder if there is a reason, or this was just overlooked.
You can check the number of columns you got. For example, the code below would skip any lines with not enough fields for your case:
if line.size() < 2:
continue
And this code will specifically target the extra line:
if file.eof_reached() and line.size() == 1 and line[0] == "":
break

Matlab: concatenation of 'n' vectors with data from 'n' .csv files within a loop. To show in a structure or table

I know there are a lot of posts about concatenation of arrays, but I can't find one that I can use for my case.
I have the following code, that reads a .csv file with mixed data types (using this function), and can store the elements of one .csv file into a Vector of dimensions Nx1.
basepath = 'Unzipped\Portfolio';
files = dir(fullfile(basepath, '*.csv'));
% Pre-allocate data storage
data = cell(size(files));
N=0;
% Import each file using it's filename
for k = 1:numel(files)
data{k} = csvimport(fullfile(basepath, files(k).name)); %reads all the data
[a, b]=size(data{k}); %the size of our data matrix
Index{k}=a-1; %Size of one .csv
N=N+Index{k}; %Size of all .csv
%for single .csv
V1s{k}=cell(Index{k}, 1);
V2s{k}=cell(Index{k}, 1);
V3s{k}=cell(Index{k}, 1);
V4s{k}=cell(Index{k}, 1);
%for all .csv
V1=cell(N,1);
V2=cell(N,1);
V3=cell(N,1);
V4=cell(N,1);
end
for i = 1:numel(files)
%Filling out every vector for a single .csv file and showing it up
[pathstr,name,ext] = fileparts(files(i).name);
V4s{i}(1:Index{i},1)=cellstr(name); %this vector contains only the name of the file repeatedly
V1s{i}=data{i}(2:end, 15);
V2s{i}=data{i}(2:end, 14);
V3s{i}=data{i}(2:end, 9);
C{i}=[V4s{i} V1s{i} V2s{i} V3s{i}] ;
Table{i}=array2table(C{i}, 'VariableNames', {'V1s' 'V2s' 'V3s' 'V4s'});
%For all .csv's
%This is where my doubts are and I know this one won't produce me any results.
V4=cat(1,V4s{:});
end
My problem comes when I have to make the concatenation of all the vectors one by one. It would be simple if I had, let's say, 3 of 'em.
I could use something like C=[V1; V2; V3]. But I can't find a way to make them all do that.
I was thinking about using counters and start indexing for every i, something like this:
V1(a:b,1)=V1s{i}
With values a and b being counter. But I don't how to write them.
I want a massive vector Nx1 with all of the V{i} going right after the next set of values.
Any help will be appreciated.
If you use the {:} operator, it will expand all of the contents of the cell array as input arguments. So if we call cat with the input data as a cell array, we can do the following to achieve what you want.
V1 = cat(1, V1s{:});
This is effectively the same as doing
V1 = [V1s{1}; V1s{2}; V1s{3}, ..., V1s{n}]
If I understood your question correctly (to read multiple csv files and store them into a N x 1 vector):
C = []; //start with an empty vector
for ... //for each csv file
M = csvread(filename); //read the csv file into a matrix
V = reshape(M, [numel(M) 1]); //reshape the matrix into an nx1 vector
C = vertcat(C, V); //concatenate nx1 vectors into Nx1 vectors
end
There are multiple ways to loop through your csv files. Either loop through an array of filenames, or you can obtain all the .csv files in the directory with:
files = dir('*.csv');
and loop through the csv files like that:
for i=1:length(files)
filename = files(i).name;
...
end
I had to work on an 'alternative' solution. I'm sure there's an easier way. But I'll settle with this one just for now.
I tried this approach: Indexing with counters. For that, you have to define those counters. So, for every different V{i}, you can index those values within a certain range.
To understand this in a better way, you have to add the following part to the last loop in my code above. Obviously, you must say first that counters are equal to zero outside of the loop so it can take an effect.
m=m+Index{i};
n=m-Index{i}+1;
V4(n:m,1)=V4s{i};
Remember, you have to place it below the commentary line that says
% For all .csv's
% This is where my doubts are and I know this one won't produce me any results.
And initialize m and n before getting into the loop:
m=0;
n=0;
You can apply this approach for indexing really large vectors or concatenating several of them.

re-initializing awk array created by split

I'm trying to use split to reverse the order of characters in a string that appears as the second field in a file with many such lines. The command:
{
n=split($2,arr," ");
for(i=1;i<=n;i++)
s=arr[i] s
}
{ print s }
does this for one line. However, the arr array (and n) seem immortal, so that when I embed this code into an awk script to process multiple lines, the output corresponding to the field I want reversed accumulates (and reverses) all previous lines:
1_B.pdb
GGTGYPGLKDKDDNEGTKYNKLLNATLIVTDVGNTIRTECPDVNRG
AARS_0001_B.pdb
GGTGYPGLKDKDDNEGTKYNKLLNATLIVTDVGNTIRTECPDVNRGGGTGYPGLKDKDDNEGTKYNKLLNATLIVTDVGNTIRTECPDVNRG
AARS_0002_B.pdb
GLILYDGFLDKRDLEGLKYNDILNRTKDVTDVGNTTRTECPDVNRKGGTGYPGLKDKDDNEGTKYNKLLNATLIVTDVGNTIRTECPDVNRGGGTGYPGLKDKDDNEGTKYNKLLNATLIVTDVGNTIRTECPDVNRG
AARS_0003_B.pdb
DGCSLDGFTDDRDLKGALYNKILNKTLIVTDVGNTTRTEVCEKDRYGLILYDGFLDKRDLEGLKYNDILNRTKDVTDVGNTTRTECPDVNRKGGTGYPGLKDKDDNEGTKYNKLLNATLIVTDVGNTIRTECPDVNRGGGTGYPGLKDKDDNEGTKYNKLLNATLIVTDVGNTIRTECPDVNRG
This appears to me to be a problem with re-initialization. I've tried to delete all previous elements of arr[] and to reset n to 0, without any effect. What do I need to do?
It's not arr that's immortal, it's s since you never [re-]init it to "" outside of the loop. arr is getting re-inited on every call to split().
Try this:
{
n=split($2,arr,/ /)
s=""
for(i=1;i<=n;i++)
s=arr[i] s
print s
}
The 3rd arg for split(), by the way is a field separator, not a string, and a field separator is a regexp with a couple of extra properties so the correct way to call split with a fixed "string" is using RE delimiters split($2,arr,/ /), not string delimiters split($2,arr," "). It doesn't make a functional difference in this case but it does when the field separator gets more complicated so best to get used to doing it the right way.
Bonus round: you would not need to explicitly re-init s if you put that code in a function:
function rev(str, arr,n,s,i) {
n=split(str,arr,/ /)
for(i=1;i<=n;i++)
s=arr[i] s
return s
}
...
{ print rev($2) }
Reason left as an exercise :-).

Trying to put a unique number code in all of my arrays

Say I have 3 arrays of arrays and when I write in any of them my counter goes up by one every insertion I do.How can I show on screen the data I inserted the same order I inserted them with that unique code it was printed when I was inserting them?
array ar1=new array[3];
array2 ar2=new array2[3];
array3 ar3=new array3[3];
I'm working on C# with 6 files. My main calls methods from the other 3 files. My base abstract class calls a counter (named code) from a constructor, which is being inserted on every array.My main is without Heredity, but calls the variables from the 3 files that are connected with heredity to the abstract base class.
This is my main and how I fill my first array.Same with different variables on the other two arrays.
i ++;
ar1[i] = new array();
{
Console.WriteLine("Inserting on first array");
Console.WriteLine("\n\nCode :{0}",code);
ar1[i].Inserting_data(); // this is the method where I fill my arrays
}
And this is how I try to call my previous insertions using a method again.
for (int x = 0; x <= i; x++)
{
Console.WriteLine("code :{0}",code); //trying to print the unique code but it prints total.
ar1[x].Print_data(); //the method for printing my insertions
}
If I leave the program like this when I insert data(up to 3 on every array) My counter is working fine and when I print them on screen, it prints them on the correct order but in the Console.WriteLine that there should be the unique code I want for every insertion (from 1 to 9 on that example, if I fill them all),my counter shows me the total insertions I have.
If I try rather than printing my counter, printing each variable from the For command,I get numbers from 0 to 2 max since that's what I put on my For. I can't unite my 3 arrays since each of them holds different variables and is on 3 different files.
Hope that helped.
I found the problem at last..
I had to add another variable on the base abstract class.Inside the constructor I had to write that the new variable (say s)was (s=+1+count++) and add it with a property(set,get) on my main.It worked fine.
Thanks for the time it took you to see this topic and ask some questions to help me.

problem with array elements in awk not being stored

I've been using awk to process hourly weather data, storing 10 arrays with as much as 8784 data elements. If an array is incomplete, i.e., stops at 8250, etc., after the "END" command I fill the remaining array elements with the last available value for the array. However, when I then print out the complete arrays, I get 0's for the filled values instead. What's causing this?? Does awk have a limit in the array size that's preventing it from filling the arrays? Following is a snippet of the awk program. In the two print statements, the first time the array elements are filled, but the second time they're empty.
Any help is appreciated because this problem is holding up my work.
Joe Huang
END{
if (lastpresstime < tothrs)
{
diffhr = tothrs - lastpresstime
for (i=lastpresstime+1;i<=tothrs+1;i++)
{
xpressinter[i]=diffhr
xpressrecords[i]=diffhr
xipress[i]=lastpress
xpressflag[i]="R"
printf("PRS xipress[%4d] =%6.1f\n",i,xipress[i]) > "ncdcfm3.prs"
printf(" xipress[%4d] =%6.1f%1s\n",i,xipress[i],xpressflag[i])
}
for (i=1;i<=tothrs+1;i++) printf("PRS xipress[%4d] =%6.1f\n",i,xipress[i])
}
~
I don't have the rep to edit your post, but here's the formatted code:
END {
if (lastpresstime < tothrs) {
diffhr = tothrs - lastpresstime
for (i=lastpresstime+1;i<=tothrs+1;i++) {
xpressinter[i]=diffhr
xpressrecords[i]=diffhr
xipress[i]=lastpress
xpressflag[i]="R"
printf("PRS xipress[%4d] =%6.1f\n",i,xipress[i]) > "ncdcfm3.prs"
printf(" xipress[%4d] =%6.1f%1s\n",i,xipress[i],xpressflag[i])
}
for (i=1;i<=tothrs+1;i++)
printf("PRS xipress[%4d] =%6.1f\n",i,xipress[i])
}
}
Note that I added a matching brace at the end.
I don't see any inherent problems in the code, so like jhartelt, I have to ask - are all of the variables properly defined? We can't tell from this sample how lastpresstime, tothrs, and lastpress get their values. In particular, if lastpress isn't, you'll get exactly the behavior you described. Note that if you have misspelled it, it will be an undefined variable and therefore use the default value of 0.
With respect to William Pursell's comment, there should also be no difference in the output of xipress[i] between the three printfs (for lastpresstime<i).
As 0 is the default value for an unknown/unused numerical variable, I would ask if you are sure, that there is no mistype in the variable names used in the END block?

Resources