I have a transposed dynamic n x 2 array that is used to populate a combobox. The primary column alone is not descriptive enough to identify rows uniquely. I would like to use the row index to identify the untransposed column uniquely. Using both columns in the array could also be used for this but may prove problematic down the line. This question is closely related to this question.
I have used Me.cbo.ListIndex = 0 to retrieve the index value. Ideally, I'd like to assign the index of the row chosen in the combobox to a variable. The ultimate goal is to use the index in two ways:
For finding the correct column to use in future calculations
As a method for comparison against another combobox that uses the same array in order to ensure that the same row has not been chosen in both comboboxes
To visually illustrate the above, the original data looks like this:
a b c b
1 2 3 4
A B C B
The transposed array looks like this:
A 1
B 2
C 3
B 4
I would like to be able to make a distinction between selecting B2 and B4, ideally by preserving and comparing index 1 and 3 respectively (0-based).
ListIndex is from the documentation. There is no documentation that I could find about retrieving the index from the name except where the value in the selection is unique. Any Help is greatly appreciated
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
}
Sorry for this question, but I haven’t found the answer in any of the texts or sites I’ve been researching. I am trying to do something that seems like it should be easy, but I don’t understand enough about arrays to pull it off. I am trying to create an array that is some number of rows; let’s say 10 rows, by 3 columns, or Myarr(1 to 10 , 1 to 3) – and then populate it as follows in memory before pasting it back into an excel sheet. Here’s an example using very simple constants and functions, not the ones I really need to run.
The reason is that I've found that running my particular construct as set of Excel formulas and VBA custom functions is very slow and results in a recalculation problem that I have written about in this forum that is not yet solved, so I am trying a work-around that performs all operations in an array, and then just pastes the result back to Excel.
Column 1 is just the list of numbers 1 to 10
Column 2 is the value of the previous row of Column 2 plus a constant; “Constant”; this is the part I really am puzzled by
Column 3 is just a function of the value of this row of Column 2
For example:
Constant = 2
Function of Column 2 value is simply Column 2 value x 4
So the output should be
Value col 1, previous value col 2 + Constant, column 2 x 4 as follows:
1,2,8
2,4,16
3,6,24
4,8,32
5,10,40
6,12,48
7,14,56
8,16,64
9,18,72
10,20,80
I just cant find any instructions about how to refer backwards to previous row values in an array and use them to produce a new value for that same column,
The simplest example would be a 1 dimensional array making a list of numbers where you started with a number and each successive row was the previous value + 1.
I realize this is probably basic stuff, but I must be searching on the wrong term to find an answer so I turn to you. Thank you very much for your help.
Did you try something like
Myarr(i,2)=Myarr(i-1,2)+const
trying to do a formula only calculator and trying to avoid a bunch of nested ifs in "hidden" columns.
Basically this is a calculator for COC type of game.
So here is a rough table (not sure how to best make a table here):
A B K M
1 name current level Max Level Next level cost
6 Soldier 2 4 X
where x=IF($K6=B6,0,VLOOKUP($A6,Masterdata!$A$4:$CD$50,32+B6,0)))
the masterdata sheet is just the name some data across several columns, some more data across several columns, and then individual columns that contain a cost starting on column 32.
this works to find the cost of the next level (3) but I would like to do a sum with something like: IF($K6=B6,0,sum(VLOOKUP($A6,Masterdata!$A$4:$CD$50,{32+B6, 33+B6,34+b6},0)))). I can get the array to work if I put in fixed numbers ie, {35, 36, 37} but not {32+B6, 33+B6,34+b6}. Additionally, the array won't dynamically change. such as current level=1, max level=9 will require a larger array than current level =5 max, max level=9. Is there a way of doing this?
Further explanation of 2nd part:
example:
current level =1 max level =9
the formula would be something like: IF($K6=B6,0,sum(VLOOKUP($A6,Masterdata!$A$4:$CD$50,{32,33,34,35,36,37,38,39},0)))) I would require 8 columns in the array to be added.
But if current level is 6 and max level is 9 then it would look something like this:IF($K6=B6,0,sum(VLOOKUP($A6,Masterdata!$A$4:$CD$50,{37,38,39},0))))
I would only require 3 columns returned in the array to be added.
So even if the formula needs to remain IF($K6=B6,0,sum(VLOOKUP($A6,Masterdata!$A$4:$CD$50,{32,33,34,35,36,37,38,39},0)))) I would like to constrain the results to only columns 37,38,39
You should exclude the cell reference from the array of constants. And you will require SUMPRODUCT (or else SUM with CSE) to coerce an array of returns:
=IF($K6=B6,0,SUMPRODUCT(VLOOKUP($A6,Masterdata!$A$4:$CD$50,{32,33,34}+B6,0)))
I'm afraid I don't understand your second query re "the array won't dynamically change".
Regards
So I abandoned the vlookup and went the index match route:
Came up with: =SUM(INDEX(Masterdata!$A$1:$Z$32,MATCH($A26,Masterdata!$A$1:$A$32,0),B26+1+14):INDEX(Masterdata!$A$1:$Z$32,MATCH($A26,Masterdata!$A$1:$A$32,0),E26+14))
Seems to work great.
Basically I am finding the row number that matches my search criteria:MATCH($A26,Masterdata!$A$1:$A$32,0) = 26
I then enter that into an index with an offset of my first column based off my min value:index(range,26,B26+1+14), along with the max value:index(range,26,E26+14) and then sum them together sum(index1:index2)
I have a variable name subject. For each unique subject there are 240 response latency recorded. Depending on that experimental condition is counterbalanced between-subject. Now I want to read the subject ID (variable name subject) and if they are even I should assign order to be 1 or if the subject ID is odd, I should assign variable order 2. Now this assignment should be done for each rows (ie 240 per subject)
I used if loop: The error I get is.... the condition has length > 1 and only the first element will be used
I also tried ifelse like this:
ifelse(data1$subject%%2==1, data1$order<-1, data1$order<-2)
Though the output is generated but it is not recorded/stored in the variable order.
Please help to make this happen.
I got the answer luckily.
the same ifelse will work in the following manner:
order<-ifelse(data1$subject%%2==1,1,2)
To include the new vector into the dataframe, we can use:
data1<-cbind(data1,order)