replacing r variables with multiple or different values - loops

I have a dataframe (raw) that can have one variable (iv1) with NA's in it. I want to replace the NA with different random values from the distribution of existing scores within (iv1), not one single value. the sample size (n) can be anything - 100 to 1000.
I save the distribution to a new data frame (dbmi) because I want to keep raw and dbmi separate, and calculate the mean and SD of the existing values of iv1 within dbmi. The following code works but replaces all of the NA's with just one value. I think I need to set up a for loop? Some kind of loop that finds the next occurrence of an NA and runs the new 'rnorm' value and sticks it in and goes to the next and does it again etc etc but I cant figure out how to do that. Any help?
dbmi<-raw
attach(dbmi)
rawmean<-mean(dbmi$iv1,na.rm=TRUE)
rawsd<-sd(dbmi$iv1,na.rm=TRUE)
for (i in 1:n){
dbmi$iv1[is.na(dbmi$iv1)]<-rnorm(1,rawmean,rawsd)
}

I actually solved my own problem. I set up the variable locations [i] that had the NA's into a variable called 'pull', then I just created a new stream into a variable called 'new' I used this code to substitute.
dbmi<-raw
attach(dbmi)
rawmean<-mean(dbmi$iv1,na.rm=TRUE)
rawsd<-sd(dbmi$iv1,na.rm=TRUE)
new<-rnorm(num,rawmean,rawsd)
for (i in 1:n){
dbmi$iv1[pull]<-new
}

Related

Is there a way to append several values to a Pine Script array in a single call?

I want to have a function within which several if conditions looking for specific integer values.
The function in some situations might be called several times in sequence in order to return the resulting values of several different if conditions.
The idea I had for this, in order to reduce the code's bulkiness, was to have the portion of the script executing the function as required run through an integer array whose each stored value is sent to the function.
But in order for this to reduce the amount of bulky code I need a way to add several values at once rather than having the values each added separately.
Is that possible in Pinescript? Or perhaps you have a different solution for executing such function?
To answer my own question, it seems a simple way to update an array to a set of new values is to use the built in function array.from.
var array<int> arr = array.new<int>(0, 0)
arr := array.from(1, 4, 10)
It doesn't even require clearing and appending the new values as it simply replaces the testArr array with the new input.
If one wants to append several additional values to an existing array rather than replacing all of the original values, it can be done like this:
arr := array.concat(arr, array.from(1, 4, 10))
If I understand your question correctly you would like a single mass update (addition) to array elements? No such function exists, each element must be individually updated, ideally in a loop if you have a consistent calculation.

Storing and replacing values in array continuously

I'm trying to read amplitude from a waveform and shine a green, yellow or red light depending on the amplitude of the signal. I'm fairly new to labVIEW and couldnt get my idea that wouldve worked with any other programming language I know to work. What I'm trying to do is take the value of the signal and for everytime it updates I'll store the value of the amplitude into an index of a large array. With each measurement being stored in the n+1 index of the array.
After a certain amount of data points I want to start over and replace values in the array (I use the formula node with the modulus for this). By keeping a finite amount of indexes to check for max value I restrict my amplitude check to a certain time period.
However my problem is that whenever I use the replace array subset to insert a new value into index n, all the other index points get erased. Rendering it pretty much useless. I was thinking its the Initialize array causing problems but I just cant seem to wrap my head around what to do here.
I tried creating just basic arrays in the front panel, but those either are control or indicator arrays and can't seem to be both written and read from, its either control (read but not write) or indicate(write but not read)?. Maybe its just not possible to do what I had in mind in an eloquent way in LabVIEW. If its not possible to do this with arrays in LabVIEW I will look for a different way to do it.
I'm pretty sure I got most of the rest of the code down except for an unfinished part here and there. Its just my issue with the arrays not working as I want them too.
I expected the array to retain its previously inputted data for index n-1 when index n is inputted. And only to be replaced once the index has come back to that specific point.
Instead its like a new array is initialized every time a new index is input.
download link for the VI
What you want to do:
Transport the content of the modified array into the next iteration of the WHILE loop.
What happens:
On each iteration, the content of the array is the same. It is the content of the initial array you created outside.
To solve this, right-click the orange square on the left border of the loop, and make it a "shift register". The symbol changes, and a similar symbol appears on the right border. Now, wire the modified array to the symbol on the right. What flows out into that symbol on the right, comes in from the left symbol on the next iteration.
Edit:
I have optimized your code a little. There is a modulo function, and an IF clause can handle ranges. ..3 means "values lower or equal 3". The next case is "Default", the next "7..". Unfortunately, this only works for integers. Otherwise, one would use nested IF clauses with the < comparator or similar.

Adding numbers from 3 different CSV columns

I am newer to programming and I am trying to figure out how I can add all of the numbers from the B column together and all of the C column together and the D column together with those variables named x y and z respectively. I have been searching everywhere for an answer. All I have gotten is to read the first line of the csv file.
This is a screenshot of part of the CSV file:
Once you've read it in, you need to somehow split it up into actual columns. Check this function, that should help.
strtok()
Then you need to store it in some kind of array. Then index which elements in the array you want, and process them by using a cumulative sum. This might require a while loop until you've reached the last line of the file.
I will leave the actual code to you, but this should get you going.

Create vector of symbolic variables by name through iteration

I'm trying to create a growing vector inside a for-loop, i.e. the vector is initialized with one element (lets call it q_1) and in the next iteration of the loop the vector is expanded by a second element (lets call it q_2) and so on. Every element is a symbolic element depending on another independent element (in my case that's the time t).
The overall idea is to create a growing second order ODE that I want to solve and plot in every loop.
The first step I already implemented, is the for-loop but I struggle with creating a new symbolic variable inside the loop. My first idea was to create the variable by name so I can use the iteration index i of the loop and create a new unique string every loop, but I don't know how to accomplish this with Maxima.
There are various ways.
use subscripted symbolic variables, e.g., u[1], u[2], u[3], ....
use gensyms (generated symbol names which are guaranteed to be different from any other existing symbol. E.g. gensym() returns something like g4282, then next time gensym() returns g4283, etc.
construct a symbol via concat, e.g. concat('foo, 3) returns foo3. THIS IS THE SOLUTION OF LAST RESORT. TRY ANYTHING ELSE BEFORE RESORTING TO CONCAT. It is too easy to make a mess that way.
If you show the code you have so far, maybe I can give more specific advice.
Building upon the accepted answer, here is an example (using the concat method) of a for loop creating a matrix M with "indexed" symbols e_ij as elements:
M: zeromatrix(6,6)$
for r: 1 thru 6 do (
for c: 1 thru 6 do (
M[r][c]: concat('e_,r,c)
)
)$

Creating an XY graph in labview which would store previous values

Link to the vi: see xy_plot_problem_withcase
In the attached vi (xy_plot_problem_updated.vi) I am able to get 3 individual values x, y and z in an array, element 0 being x, element 1 being y and element 2 being z.
These three values come for every iteration of the outer while loop. I would like to store all generated x values into one array and same with y and z so I can use the final arrays to generate one final graph.
The outer while loop runs 30 times and I would like to record the 30 different values generated at index 0 in a separate array. I tried using a shift register, build array etc but its just replacing element 1 (of the new array) with the newest element generated (They are not getting accumulated).
I have encountered this problem while designing for a system which records 3 different readings for every 5 degree increase in temperature. I want to be able to plot the acquired values against the current temperature. Hence, the outer while loop is actually a case statement which gets triggered every time the temperature goes up by 5 degrees.
I have also attached the main VI too alongside (final.vi).
Any help appreciated!!
Thanks in advance!!!
In your final.vi you have a while loop, you should move everything in the case into the while loop. My advise for you would be to look at the LabVIEW fundamentals on data flow and on shift registers.
In your code you were resetting the shift register in the while loop every iteration.
Try to clean up your code and use the executing highlighting function (the light bulb).

Resources