I have a list of dfs with 268 dfs called z
Each df has the following str
df <- structure(list(id = structure(c(50109025, 60901029, 140103026,
50705001, 110113007, 111201026, 50521001, 111201007, 60901030,
50508026, 50508026, 110113003, 110113008, 111202009, 140103023,
50203026, 110113005, 140102088, 110104030, 140102095), label = "Identificador", format.spss = "F10.0", display_width = 10L),
name = c("ABCA1", "ABCA1", "ABCA1", "ABCA1", "ABCA1", "ABCA1",
"ABCA1", "ABCA1", "ABCA1", "ABCA1", "ABCA1", "ABCA1", "ABCA1",
"ABCA1", "ABCA1", "ABCA1", "ABCA1", "ABCA1", "ABCA1", "ABCA1"
), `1` = c(1.063, 1.623, 1.078, 1.765, 1.596, 0.365, 0.53,
0.672, 1.77, 0.875, 0.875, 1.426, 0.569, 0.921, 0.988, 1.017,
1.208, 0.642, 0.703, 0.834), `3` = c(0.753, 2.05, 2.132,
1.961, 1.165, 0.764, 1.983, 1.59, 1.028, 1.488, 1.488, 2.226,
1.16, 0.967, 0.95, 1.761, 1.488, 0.871, 5.634, 0.898)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -20L))
What I am trying to achieve is compare by paired t.test (according to ID) on each df the columns 1 and 3
#1st approach
names <- names(z)
rowvars <- c(names)
rowvars <- base::sort(rowvars)
for (v in 1:length(rowvars)) {
P1 <- t.test(x= z[,v[3]][, "1"], y= z[,v[4]][, "3"], paired = TRUE)$p.value
}
# 2nd approach
lapply(z, function(x){t.test(x = z[[x]][, "1"], y = z[[x]][, "3"], paired = TRUE)
})
# purrr approach
I am pretty sure that purrr can also be useful to do it, but my attempts are lame to post them
The 2 first attepmts are throwing erros that shouldn't be there (I guess, at least checking the str of my database)
Thanks in advance
Related
I'm currently trying to manipulate an array of objects that takes a date, double, and string values for the whole year. Im trying to group/add all items that share the same week and name. Currently it doesn't do that, it just adds up all the qty of each item for the week and ends up printing that output twice.
I've tried using multiple .mapvalues and got close to what my goal, but I'm confused how to add/group together items that share the same week and name for each location.
Current OutPut:
["Location1": ["Location1": [1: 12.0], "Location#2": [3: 16.0]], "Location2": ["Location1": [1: 12.0], "Location#2": [3: 16.0]]]
Goal:
["Location1": [1: ["Apple": 5, "Pear": 7]], "Location2": [2: ["Apple": 7, "Pear": 9]]
//["Location": weekOfYear: ["itemName": qtySold]]
import Foundation
let cal = Calendar.current
extension Date {
var week: Int {
return cal.component(.weekOfYear, from: self)
}
var weekAndYear: DateComponents {
return cal.dateComponents([.weekOfYear, .yearForWeekOfYear], from: self)
}
var yearForWeekOfYear: Int {
return cal.component(.yearForWeekOfYear, from: self)
}
}
struct sortedDates {
var date: Date
var itemName: String
var itemSold: Double
}
//Current Dates in String Form
let date1 = "\(2023)-\(01)-\(04)"
let date2 = "\(2023)-\(01)-\(05)"
let date3 = "\(2023)-\(01)-\(06)"
let date4 = "\(2023)-\(01)-\(17)"
let date5 = "\(2023)-\(01)-\(18)"
let dateFormatter = DateFormatter()
//Dates in String Form converted to Date
dateFormatter.dateFormat = "yyyy-MM-dd"
let dateForm1 = dateFormatter.date(from: date1)
let dateForm2 = dateFormatter.date(from: date2)
let dateForm3 = dateFormatter.date(from: date3)
let dateForm4 = dateFormatter.date(from: date4)
let dateForm5 = dateFormatter.date(from: date5)
//locations and Items
let allLocations = ["Location1", "Location2"]
let allItems = ["Apple", "Pear"]
//List of Data
let allInfo: [String: [sortedDates]] = ["Location1": [sortedDates(date: dateForm1 ?? Date(), itemName: "Apple", itemSold: 3.0), sortedDates(date: dateForm2 ?? Date(), itemName: "Pear", itemSold: 7), sortedDates(date: dateForm3 ?? Date(), itemName: "Apple", itemSold: 2)], "Location#2": [sortedDates(date: dateForm4 ?? Date(), itemName: "Apple", itemSold: 7), sortedDates(date: dateForm5 ?? Date(), itemName: "Pear", itemSold: 9)]]
//Dict in which final results will be held.
var tempDict: [String: Any] = [:]
//Loop through each location and groups items together
for curLocation in allLocations {
let grouppedByYearThenWeek = allInfo
.mapValues{(yearArray: [sortedDates]) -> Dictionary<Int, Double> in
return Dictionary(grouping: yearArray, by: { $0.date.week })
.mapValues{ (value: [sortedDates]) in
return value.map{ $0.itemSold }.reduce(0,+)
}
}
if tempDict[curLocation] == nil {
tempDict[curLocation] = []
}
tempDict[curLocation] = grouppedByYearThenWeek
}
print(tempDict)
I solved this by breaking it down into two problems to solve and then combined the solutions. First we have the arrays in the starting dictionary allInfo, for instance
let array = [SortedDate(date: dateForm4, itemName: "Apple", itemSold: 7), SortedDate(date: dateForm5, itemName: "Pear", itemSold: 9)]
We can sum the values per name using reduce(into:) and group by week by using Dictionary(grouping:by:)
Dictionary(grouping: array, by: \.date.week).mapValues { $0.reduce(into: [:]) { $0[$1.itemName, default: 0] += $1.itemSold} }
This would give us the correct result for that particular value in the array
[3: ["Apple": 7.0, "Pear": 9.0]]
Now we only need to apply this to each value in the dictionary
var result: [String: [Int: [String: Double]]] = [:]
allInfo.forEach { (key, value) in
result[key] = Dictionary(grouping: value, by: \.date.week).mapValues { values in
values.reduce(into: [:]) { $0[$1.itemName, default: 0] += $1.itemSold }
}
}
["Location1": [1: ["Apple": 5.0, "Pear": 7.0]], "Location2": [3: ["Apple": 7.0, "Pear": 9.0]]]
expected output is = ['2', '12']
current output is = ['2', '1', '2']
#program
def listToString(s):
str1 = " "
print(s)
new_str = str1.join(s)
l = []
for i in new_str:
if i.isdigit():
l.append(i)
return l
s = ['abc2', 'is the', 'way12', 'for']
print(listToString(s))
Something like this.
import re
def listToString(str_list):
print(s)
all_numbers = []
for str_ in str_list:
all_numbers = all_numbers + re.findall('[0-9]+', str_)
return all_numbers
s = ['abc2', 'is the', 'way12', 'for']
print(listToString(s))
Output:
['abc2', 'is the', 'way12', 'for']
['2', '12']
I want to performance test a recommender system.
It is important that the sequence of each product request for the same customer_id is respected as it influences the CPU load of the system under test.
How should I approach this? (This is what I have so far the exec does not work in the foreach)
import io.gatling.core.Predef._
import io.gatling.http.Predef._
class MySimulation extends Simulation {
val baseUrl = "http://127.0.0.1:8080" // http://127.0.0.1:8080/recommend/144/9200000033418652/
val contentType = "application/json"
var realtime_feeder = Array(
Map("product_ids" -> List("9200000118431183", "9200000118431213", "9200000089631081"), "customer_id" -> "1"),
Map("product_ids" -> List("9200000121305523"), "customer_id" -> "2"),
Map("product_ids" -> List("9200000118431349", "9200000089631025"), "customer_id" -> "3"),
)
val httpConfiguration = http.baseUrl(baseUrl)
.acceptHeader(contentType)
.contentTypeHeader(contentType)
.shareConnections
.warmUp(baseUrl)
val productRequest = http("Recommend ${customer_id} ${product_id}")
.get("/recommend/${customer_id}/${product_id}")
val scn = scenario("scenario")
.feed(realtime_feeder)
.foreach(session => {
val product_ids = session("product_ids").as[List[String]]
val customer_id = session("customer_id").as[String]
for (product_id <- product_ids){
exec(productRequest, customer_id, product_id)
}
},
)
setUp(
scn.inject(
atOnceUsers(2)
).protocols(httpConfiguration))
}
Read the documentation first. You have serious misunderstanding of Gatling's programming model.
For knowing what to put into foreach, read Expression and EL.
foreach requires an Expression[Seq[Any]] and you can create one with the expression language (EL). by writing "${product_ids}". This will fetch the session attribute product_ids from the virtual users' session.
foreach("${product_ids}", "product_id") {
// Write your actions that use `product_id`.
// E.g. use EL to create a JSON
}
BTW, kudos to you for knowing how dynamic payloads are important to making a load test realistic.
Thanks George Leung for his suggestions. Here is my working end result.
import io.gatling.core.Predef.{exec, _}
import io.gatling.http.Predef._
import scala.concurrent.duration._
class MySimilation extends Simulation {
val baseUrl = "http://127.0.0.1:8080" // http://127.0.0.1:8080/recommend/144/9200000033418652/
val contentType = "application/json"
var realtime_feeder = Array(
Map("product_ids" -> List("9200000118431183", "9200000118431213", "9200000089631081"), "customer_id" -> "1"),
Map("product_ids" -> List("9200000121305523"), "customer_id" -> "2"),
Map("product_ids" -> List("9200000118431349", "9200000089631025"), "customer_id" -> "3"),
).random
val httpConfiguration = http.baseUrl(baseUrl)
.acceptHeader(contentType)
.contentTypeHeader(contentType)
.shareConnections
.warmUp(baseUrl)
val productRequest = http("/recommend/${customer_id}/${product_id}/")
.get("/recommend/${customer_id}/${product_id}/")
val scn = scenario("scenario")
.feed(realtime_feeder)
.foreach("${product_ids}", "product_id") {
exec(productRequest).pause(1 seconds, 2 seconds)
}
setUp(
scn.inject(
nothingFor(5.seconds),
constantUsersPerSec(5) during (1 minutes)
).protocols(httpConfiguration))
}
The process I want to achieve here is to get data into a 2D array, filter it out, then only grab selected column data within the filtered array and push it back into a new tab in the same google sheet. Each process is commented, so you can follow what I am doing.
function copyToAndFrom() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s1 = ss.getSheetByName("PendingX");//sheet with imported data
var s2 = ss.getSheetByName("Resolved");//archive sheet
var lr = s2.getLastRow()+1;//get last row of archive sheet + 1
var dList = s1.getRange("PendingX!G2:G").getValues(); // Gets all the values of that specific column
var dLast = dList.filter(String).length; //Gets the length of dList.
var myArray = []; // A 2D array. I want a 12 column, dLast number of rows array.
myArray.length = dLast;
var tempArr = []; // Secondary array to get the filtered data from myArray array.
var filteredArr = []; // Get only specific data from tempArr array.
// myArray = s1.getRange('PendingX!D2:O').getValues();
// The the above comment is the original sheet import with 400+ rows but for testing, Ive disabled it and assigned manually as below.
myArray = [["Pending", , "abc", "def", "ghi", "jkl", "mno", "pqr", "stu", "vwy", "xzz", "aaa"],
["Resolved", , "aabc", "adef", "aghi", "ajkl", "amno", "apqr", "astu", "avwy", "axzz", "aaaa"],
["Pending", , "babc", "bdef", "bghi", "bjkl", "bmno", "bpqr", "bstu", "bvwy", "bxzz", "baaa"]];
var myrowindex, myString, mycolumnindex;
tempArr[myrowindex] = [];
for (myrowindex in myArray) { //Loops through the number of rows in myArray.
myString = myArray[myrowindex][0];
if (myString === "Pending") {
for (mycolumnindex in myArray[myrowindex]) { //Loops through each column of a row in myArray.
var myColumnValue = myArray[myrowindex][mycolumnindex]; //Loads each component of myArray to a string.
tempArr[myrowindex].push(myColumnValue); //Push the string into the tempArr array.
}
myColumnValue = null;
myString = null;
}
}
for (myrowindex in tempArr){ //Load data from columns 0,3,10 and 11 into filteredArr array.
filteredArr.push(tempArr[myrowindex][0]);
filteredArr.push(tempArr[myrowindex][3]);
filteredArr.push(tempArr[myrowindex][11]);
filteredArr.push(tempArr[myrowindex][12]);
}
Logger.log(tempArr.length); // must be less than myArray.length but it is not.
Logger.log(tempArr[0].length); // must be equal to myArray[0].length but it is not.
Logger.log(tempArr); //Doesnt store data in a 2D Array but has to.
//CODE FOR STORING DATA TO SHEET:
for (var row in filteredArr) { //Loops through the number of rows in filteredArr.
for (var col in filteredArr[row]) { //Loops through each column of a row in filteredArr.
var myColumnValue = tempArr[row][col]; //Loads each component of filteredArr to a string.
final = s2.getRange(2,1,row,col).setValue(myColumnValue); //Push the string into the sheet.
}
}
}
Error after recent changes:
TypeError: Cannot call method "push" of undefined. (line 30, file "CopyToFromScript")
CURRENT LOG OUTPUTS AND EXPECTED LOG OUTPUTS:
Current Logger for tempArr.length:
[19-02-01 19:25:08:933 IST] 22.0
Expected Logger for tempArr.length:
[19-02-01 19:25:08:933 IST] 2.0
Current Logger for tempArr[0].length:
[19-02-01 19:25:08:934 IST] 12.0
Expected Logger for tempArr[0].length:
[19-02-01 19:25:08:933 IST] 12.0
Current Logger for tempArr:
[19-02-01 19:25:08:935 IST] [Pending, abc, def, ghi, jkl, mno, pqr, stu, vwy, xzz, aaa, Pending, babc, bdef, bghi, bjkl, bmno, bpqr, bstu, bvwy, bxzz, baaa]
Expected Logger for tempArr:
[19-02-01 19:25:08:935 IST] [[Pending, abc, def, ghi, jkl, mno, pqr, stu, vwy, xzz, aaa], [Pending, babc, bdef, bghi, bjkl, bmno, bpqr, bstu, bvwy, bxzz, baaa]]
Current Logger for filteredArr:
[19-02-01 19:24:35:773 IST] [P, d, null, null, a, null, null, null, d, null, null, null, g, null, null, null, j, null, null, null, m, null, null, null, p, null, null, null, s, null, null, null, v, null, null, null, x, null, null, null, a, null, null, null, P, d, null, null, b, c, null, null, b, f, null, null, b, i, null, null, b, l, null, null, b, o, null, null, b, r, null, null, b, u, null, null, b, y, null, null, b, z, null, null, b, a, null, null, null, null, null, null]
Expected Logger for filteredArr:
[19-02-01 19:24:35:773 IST] [[Pending, def, xzz, aaa], [Pending, bdef, bxzz, baaa]]
I was able to solve the problem. It was associated with properly defining the 2D Arrays within the loops. I have included the fixed portion of the code for anyone who'd like to check.
var myrowindex, myString, mycolumnindex, validmyrowindex = 0;
for (myrowindex in myArray) {
myString = myArray[myrowindex][0];
if (myString === "Resolved") {
tempArr[validmyrowindex] = [];
for (mycolumnindex = 0; mycolumnindex < 12; mycolumnindex++){
if(tempArr[validmyrowindex] != undefined) {
var myColumnValue = myArray[myrowindex][mycolumnindex];
tempArr[validmyrowindex][mycolumnindex] = myColumnValue;
} else {
var myColumnValue = myArray[myrowindex][mycolumnindex];
tempArr[validmyrowindex] = myColumnValue;
}
myColumnValue = null;
myString = null;
}
} else {validmyrowindex--}
validmyrowindex++
}
Everything below this point from the original code is redundant. It was replaced with an iteration of the above with fixed columns. And the basic setValues(array) function was used for storing the entire array into the sheet. Thanks for your valuable explanation and support, everyone.
I am new at iOS development. I am trying to do the following:
Store a title, that allows any number of subcategories to be added below it. Each subcategory needs to have 2 integers attached to it. I need to be able to use, edit, remove, and add new titles, subcategories, and integers.
"Title":
"Subcategory": int, int
"Subcategory": int, int
"Title":
"Subcategory": int, int
"Subcategory": int, int
"Subcategory": int, int
"Subcategory": int, int
I have tried several times with structs and arrays.
For example:
struct everyThing {
var titles = ["Title 1", "Title 2", "Title 3"]
var subcategories = ["subcat1", "subcat2", "subcat3", "subcat4", "subcat5"]
var integers: [Double] = [5.0,10.0,7.0,15,3,6,7,8,12,14,13,15]
}
struct grouping1{
var title = everyThing().titles[0]
var subcategories = everyThing().subcategories[0..<2]
var integers = everyThing().workRest[0..<2]
}
struct grouping2{
var title = everyThing().titles[1]
var subcategories = everyThing().integers[2..<4]
var integers = everyThing().integers[2..<4]
}
It becomes impossible to keep track of and scale, as any number of subcategories can be added under a particular title.
Any ideas of the best way to organize this data? Let me know if this is too vague.
You can use a dictionary [String : [String : (Int, Int)]]
let dictionary: [String : [String : (Int, Int)]] = [
"Title1" : [
"subcat1" : (5, 10),
"subcat2" : (7, 15)
],
"Title2" : [
"subcat3" : (3, 6),
"subcat4" : (7, 8),
"subcat5" : (12, 14)
]
]
To get the tuple of integers (Int, Int) under a category and subcategory, you can use
let tuple: (Int, Int) = dictionary[title]![subcategory]!
But, this uses forced unwrapping using the !. Instead, a safer way to do it that won't cause your app to crash would be
let tuple: (Int, Int)? = dictionary[title]?[subcategory]
Then, to get the values in the tuple you could use
let val1: Int? = tuple?.0
let val2: Int? = tuple?.1
To just set the value 0 instead of nil when the value does not exist, you could use the ?? operator
let val1: Int = tuple?.0 ?? 0
let val2: Int = tuple?.1 ?? 0
If you wanted to loop through all the values, it could be done with
for title in dictionary.keys{
for subcategory in dictionary[title]!.keys{
//we can force unwrapping because we are sure the
//value will not be nil, because we are looping
//through the keys of the dictionary
let value1: Int = dictionary[title]![subcategory]!.0
let value2: Int = dictionary[title]![subcategory]!.1
//use title, subcategory, value1, and value2 as you please
}
}
Setting a value is as simple as
dictionary["newOrExistingTitle"]["newOrExistingSubcategory"] = (num1, num2)
For example
dictionary["Title1"]["subcat2"] = (8, 2)
You need 2 model values
struct SubCategory {
let title: String
let value0: Int
let value1: Int
}
struct Category {
var title: String
private (set) var subcategories = [SubCategory]()
init(title:String) {
self.title = title
}
}
Let's see what you can do now.
Adding a 2 categories
var categories = [Category]()
let category0 = Category(title: "Category 0")
categories.append(category0)
let category1 = Category(title: "Category 1")
categories.append(category1)
// [Category(title: "Category 0", subcategories: []), Category(title: "Category 1", subcategories: [])]
Adding a Subcategories
var cat = categories[0]
cat.subcategories.append(SubCategory(title: "Sub0", value0: 1, value1: 2))
cat.subcategories.append(SubCategory(title: "Sub1", value0: 3, value1: 4))
categories[0] = cat
// [Category(title: "Category 0", subcategories: [SubCategory(title: "Sub0", value0: 1, value1: 2), SubCategory(title: "Sub1", value0: 3, value1: 4)]), Category(title: "Category 1", subcategories: [])]
Changing the Category Title
var cat = categories[0]
cat.title = "New title"
categories[0] = cat
// [Category(title: "New title", subcategories: []), Category(title: "Category 1", subcategories: [])]