how to retrieve data from a column and produce that value into a new column - mysql

I'm super new to R and have a question on how to do something. I listed the things that i got to work so ppl have an idea on what is going on. the thing im having trouble with is in bold.
-I have a data spreadsheet with 2 columns of data (End and CTCF). With the CTCF column having more cells
-I want to to take one value from the "End" column and subtract that value from each individual value in "CTCF" column (so i would have a bunch of products from each calculation)
-I want to then compare those products and find the miniumn absoulute value and the coresponding spot in the CTCF column
-then place that value into a new column ajacent to the corresponding End value.
I wrote a while loop (i know there is probably a WAY easier method) and got the calulation/comparison thing down. I was even able to output the location of the CTCF cell that contains my value of interest see below:
*
*data2<-read.csv("farah.csv")
head(data2)
periph_ctcfs<-list()
temp<-vector(mode="numeric", length = 356)
count<-1
for(i in 1:length(data2$CTCF)) while (count<357)
{
End<-data2$End[count]
periph_ctcfs<-(End-data2$CTCF)
periph_ctcfs<-abs(periph_ctcfs)
periph_ctcfs<-which.min(periph_ctcfs)
print(periph_ctcfs)
temp[]<-data2$CTCF[periph_ctcfs]
count<-count + 1;
}*
The problem is when im trying to produce the new "periph_ctcfs" column, when im trying to insert it into the "temp" vector, the last printed number gets placed within all the cells of the "temp" vector. It feels like that each time the loop goes through its not inserting the retrieved value into "temp". Can anyone help? Thanks ive included a link to a photo (below) so you can get a visual on the layout of the data. Sorry for being a n00b.
For clarity purposes:

Related

Pulling One Element From A CSV File

I'm trying to write a function that will return the most recent 'closing' value in a csv file containing the data of a cryptocurrency. The csv file contains 6 columns and about 900 rows and I'm looking to only pull one element of the table.
However, I seem to faced a fair bit of difficulty in pulling this off for some reason. The function below returns values from the column I want, however it seems to be pulling values from the very bottom of the document (whereas I want the most recent values).
Also, just a side note to explain what I was attempting to do with the 'count'. Since I'm expecting the value I want to be located on the second row, I wanted my for loop to only iterate through two lines of the file. However, as the result of the function went on to reveal to me, as it currently stands with the counter I'm returning two values from the function.
I understand there must be a much less convoluted way of getting the information I need so am open to any solution to the problem. Though, that being said, I'd be really interested to see where I went wrong here as I'm fairly new to Python.
Thanks a lot!
def csv_to_close(csv_file):
with open(f"{csv_file}.csv", 'r') as csvfile:
csv_file = csv.reader(csvfile)
running = True
count = 0
while running == True:
if count < 2:
for column in csv_file:
close = column[4]
count += 1
else:
running = False
print(close)

Access writng to wrong row number

4150
NRrows = RSNonResourceCosts.RecordCount ' Number of Rows in Non Resource Table
NRCols = RSNonResourceCosts.Fields.Count ' Number of Fields in NonResource Table
Dim CL(1 To 10) As Integer ' This is to count "filled rows" when spreadsheet is filled
Dim Header(1 To 10) As String
'-----------
'Find the Headers (Taken from Actual Table and not predefined as original)
For Each Recordsetfieldx In RSNonResourceCosts.Fields
If C > 0 Then
Header(C) = Recordsetfieldx.Name
End If
C = C + 1
Next Recordsetfieldx
4170
R = 0
'Write to worksheet
RSNonResourceCosts.MoveFirst
Do Until RSNonResourceCosts.EOF
For C = 1 To NRCols - 1
FieldName = RSNonResourceCosts.Fields(C).Value
If RSNonResourceCosts.Fields(Header(C)).Value <> "" Then
CL(C) = CL(C) + 1
WKS.Cells(200 + R, C) = RSNonResourceCosts.Fields(Header(C)).Value
End If
Next C
RSNonResourceCosts.MoveNext
R = R + 1
Loop
I attach code. Have solved part of original by defining Recordset. User can add column to Table. First part of code determines the headers. Second part determines values and writes to worksheet. The new Rows are appearing first on the worksheet and in wrong column. I tried attaching worksheet but it looked awful. Any help would be appreciated.
Two things:
1) The order your records is the order they are in the recordset. If you want them in a particular order, try sorting them (perhaps with an ORDER BY in the underlying SQL statement)
2) For the column issue: In the first bit of code, I don't see where C is initialized, but keep in mind the Headers and Fields both start with an index of 0, so if you set Header(1) = the first field's header (index 0), but then copy the data in the fields without shifting the index value, it will shift everything over by one column.
As an added note, you might want to consider what happens when you have more than 10 columns. Using fixed-length arrays means your code will break. You might want to read about using a dynamic array and ReDim.
I don't yet feel like I have completely grasped the entirety of the problem yet, but let me take a stab at it. From what I do understand, data is being written from your record set into excel (good), but it is going into the 'wrong row' (question title) and the 'wrong column' (question text).
From what I see, I don't know the purpose of FieldName = RSNonResourceCosts.Fields(C).Value, but I want to make sure that you understand that RSNonResourceCosts.Fields(C).Value is not necessarily equivalent to RSNonResourceCosts.Fields(Header(C)).Value. More than that, you are likely missing at least one column altogether in your output, or at least skipping over it accidentally. rs.Fields(0).name is the first 'column' in a recordset, but it is completely ignored in your code. Perhaps this is intentional, maybe it is a key field or something useless to you, but it is important that you are making that distinction intentionally. But, since I don't see where your code populates the headers in your worksheet, I wonder if 'wrong column' means every record has been shifted a column and your last column is sitting empty. That, coupled with the dubious omission of C being initialized as 0 (not 1, or anything else) in your above code, makes me concerned that Header(3) could possibly by field(1), or field(4), or I don't know. That would certainly also confuse the columns in your output, or at least make dependence on FieldName frustrating.
Another thing, really a shot in the dark: NRrows. I have had issues before, depending on how I create my recordset, of not getting the correct record count the first time. And, if I base the population of a worksheet, array, etc., on the number of rows and the records relative position in that number, my records get all sorts of wacky. Maybe you did this already, but since it isn't shown, I recommend a RSNonResourceCosts.movelast: RSNonResourceCosts.movefirst line before you define NRrows, just to be sure.
And last, if I am way off base here... then you really are going to have to show us the spreadsheet, even if it isn't your most beautiful work. We all know that if it were, you wouldn't be asking about it here... so set your pride aside, and be more specific as well as show us what the output looks like and how it should look.

Tabulate JSON into Sheets

I've been trying to get a readable database of a JSON file from a URL.
I've used fastfedora's script on Github, https://github.com/fastfedora/google-docs/blob/master/scripts/ImportJSON/Code.gs, to import JSON from the URL to Sheets. I'm using the basic:
=TRANSPOSE(ImportJSON("https://rsbuddy.com/exchange/summary.json"))
I used transpose as it was easier to work with two long columns rather than two long rows.
The data that's been imported however, is very messy: https://docs.google.com/spreadsheets/d/1mKnRQmshbi1YFG9HHg7-mKlZZzpgDME6-eGjDJKzbRY/edit?usp=sharing. It's basically 1 long column of descriptive data, (name, id, price etc.) and another column of the variable (the actual name of the item and it's price in digits).
Is it possible to manipulate the resultant Sheets page so that the common factors in the first column can be lined up with the pseudo-table beside two initial columns? E.g. for the first item, the ID will be '2', the name will be 'Cannonball', the Sp will be '5' etc.
Thanks in advance. Do forgive me for my ignorance.
Example
Simple formula
I think, faster way to get IDs:
=QUERY(QUERY(A2:B,"select B where A <> '' offset 4"),"skipping 7")
and if you want Names:
=QUERY(QUERY(A2:B,"select B where A <> '' offset 1"),"skipping 7")
when you change offset from 0 to 6, you get different columns
outputs.
7 is the number of columns in Data.
The result is autocompleted column with Data.
Hard formula
Also possible to get the whole result with one formula:
paste =COUNTA(A:A) in cell E2
paste 7 in E3, this is the number of columns in Data
=E2/E3 in E4
And then in cell G2 or somewhere on right paste the formula:
=ArrayFormula(vlookup(if(COLUMN(OFFSET(A1,,,1,E3)),
(row(OFFSET(A1,,,E4))-1)*E3+COLUMN(OFFSET(A1,,,1,E3))),
{row(OFFSET(A1,,,E2)),OFFSET(B2,,,E2)},2,0))
It works slow, but gives the whole table.
or Script?
I've also tried to use script UDF function. Here's test formula:
=ConvertTo2D(TRANSPOSE(R3:R16),7)
where R3:R16 is small range which was splited into table with 7 columns. The script is pretty short:
function ConvertTo2D(Arr, index) {
var newArr = [];
while(Arr[0].length) newArr.push(Arr[0].splice(0,index));
return newArr;
}
Sounds good. But! It is ve-e-e-e-ery slow. So This solution is good only for quick test.
If the data is structured and every object will always have the same structure you can use a simple offset to do this:
=OFFSET($B$2,
(ROW($B2) - 2) * 7 +
COLUMN(D$1) - 4,
0)
Put that in D2 and drag to the right and down.
It is possible to immediately return the data in this fashion but for that you need to meddle with the script.

Copy formula down using script

I've been working on a script (With some help from folk on SO). I have managed to create a script that imports a csv file and updates named ranges based on the imported data.
Here is an image of what I'm trying to do:
At this point I have imported data which has been populated into columns A:G. The cells in columns H:L are formula and are based on the data in A:G. Because I have imported data the data are now longer than the formula range. If not relying on script I would just double click the small blue square at the bottom right of the highlighted cells and the formulas would copy down. This is what I'd like to have the script do once I have imported the data.
I created a range called "formula_range" which automatically updates with the length of data. "formula_range" starts in cell H3 and ends in L:N where N is the length of rows in the sheet.
"formula_range" therefore contains some populated cells with formula and then blank rows all the way to the bottom of the sheet.
I saw this SO post. So in English, my line of thinking is:
Create a variable formula_range H3:L3 as a range
paste formula_range to every row in formula_range
Voila?
How do I get the variable formula_range if the first row in formula_range will always have the formula to be copied down. Put another way it should be fixed at H3:L3.
I tried this:
var copy_range = ss.getRangeByName("formula_range").getRange(1,5,1);
SpreadsheetApp.getUi().alert(copy_range);
My alert said "undefined" (I really wasn't sure what it would show.)
My line of thinking is not the most efficient since I am going to copy over existing formula. Perhaps there is a better way?
How do I get the first row in a named range formula_range[0]; ?
How can I use script to copy down formula in formula_range?
I think what you're trying to accomplish could be achieved more efficiently with a built-in arrayformula:
https://support.google.com/docs/answer/3093275
Place in H3:
=ARRAYFORMULA(DATE(A3:A,B3:B,1))
for each of your formulas in H through L in row 3 surround them with array formula and extend the arguments with open-ended ranges starting with row 3 as in the above example

Why does my Apps Script reference the wrong row?

The Goal
I am trying to create a spreadsheet using some custom functions. The purpose for the sheet is to keep score in a quizzing competition. The top row has the question number, the second row the score, and the third number the number of fouls for that question.
The Problem
After noticing some problems with the score calculation, I was able to narrow the problem down to the part of the code where I add up the fouls that occurred prior to the current question. It seems that no matter what I do, the code sums over the question row, not the foul row.
Attempted Solutions
The extremely strange thing is that no matter what I change the reference cells to, it always produces the same result (i.e. it still references the question row same as it always has).
Example
I'm not sure if that makes any sense, but I've made an example sheet here so you can see what I'm talking about and try to figure out what is going on. Keep in mind that I'm well aware that I could accomplish what I'm trying to do in the example sheet with a simple built-in formula, but since there's no way to use worksheet formulas on the Apps Script side, I have to create my own version of it. I've made the example editable for anyone with the link, so you should have full access to it; but if you have problems, just let me know, and I'll see what I can do to fix it.
In your For loop, you are summing the indexes rather than the values:
Try:
for (var PrevValue in PrevValues[0]) {
Sum = Sum + Number(PrevValues[0][PrevValue]);
}
EDIT:
You'll also need to account for the case where you pass in a single cell rather than a range (=mySum($B4:B4)), because in that case the value is passed directly instead of an array.
if(PrevValues instanceof Array){
for (var PrevValue in PrevValues[0]) {
Sum = Sum + Number(PrevValues[0][PrevValue]);
}
}else
Sum = PrevValues;