I use an R script for parameter sweeping in a NetLogo model. The script opens the model correctly with NLStart and NLLoadModel. In the sweeping loop, I store the values of input parameters by building a list:
run.params <- list(
RD = NLReport("RD?"),
RD.unif = NLReport("RD-unif?"),
Gini = NLReport("gini"),
Gamma = NLReport("gamma"),
GROUP = NLReport("GROUP?"),
SN = NLReport("SN?"),
Group.size = NLReport("group-size"),
Sn.size = NLReport("sn-size"),
W.group = NLReport("w-group"),
W.sn = NLReport("w-sn"),
Num.sn <- NLReport("num-sn"),
LF <- NLReport("LF?"),
L.memory <- NLReport("L-memory"),
LF.agents <- NLReport("LF-agents?"),
MASS.enthusiasm <- NLReport("MASS-enthusiasm?"),
W.crowd <- NLReport("w-crowd")
)
The results are as follows:
$RD
[1] FALSE
...
$Sn.size
[1] 5
$W.group
[1] 0.05
$W.sn
[1] 0.05
[[11]]
[1] 2
[[12]]
[1] FALSE
[[13]]
[1] 5
[[14]]
[1] FALSE
[[15]]
[1] FALSE
[[16]]
[1] 0.01
i.e after W.group, the values are retrieved correctly (as checked by launching NetLogo outside R) but the names are missing. I don't know why this is happening. I would be grateful for any help on that.
Related
I am using the "websocket" library to process quotes from the Binance exchange. My script processes a large amount of incoming data and finds abnormally large data in them, which it stores in a small file. I ran into a problem in the processing speed of a large data stream. This results in a significant processing delay. How can I handle such large streams efficiently? unfortunately I cannot reproduce an example here, but I would like to know about the general practices of processing large amounts of data
DataProcess <- function(message){
data <- message # I assign the incoming message to a variable
ticker <- data$s # I get from the message the name to which the data belongs
bid <- data$b # data in which I am looking for large values
ask <- data$a # data in which I am looking for large values
orderBook <- c(bid, ask) # data in which I am looking for large values
secondValuesNumeric <- as.numeric(sapply(orderBook,"[[",2)) #
maxIndex <- which.max(secondValuesNumeric) #
biggest_size <- orderBook[[maxIndex]] # 3 lines with which I find the highest value in the received message
x <- list(symbol = ticker, biggest_size)
# I create a connection to the file in which the last recorded large value is stored
con <- file(description = paste(ticker, '.txt', sep = ''))
#if the largest value in the received data is greater than the specified large value and the price at which this value is located is not equal to the last recorded one, then I rewrite the incoming message to the file and send the message to the messenger
if (as.double(x[[2]][2]) > dat$bigSize[dat$ticker == ticker] & !isTRUE(all.equal(as.double(readLines(con = con, n = 1, warn = F)), as.double(x[[2]][1])))) {
writeLines(paste(x[[2]][1]), con)
bot$send_message(chat_id = -498542337,
text = paste(ticker, ': ', 'Large size for the price: ', x[[2]][1], ' Volume: ', x[[2]][2]))
}
close(con)
}
$e
[1] "depthUpdate"
$E
[1] 1616530913906
$T
[1] 1616530913402
$s
[1] "COMPUSDT"
$U
[1] 276620494586
$u
[1] 276620494586
$pu
[1] 276620491003
$b
$b[[1]]
[1] "379.81" "0.170"
$b[[2]]
[1] "379.80" "2.200"
$b[[3]]
[1] "379.79" "0.486"
$b[[4]]
[1] "379.75" "4.269"
$b[[5]]
[1] "379.74" "1.410"
$b[[6]]
[1] "379.71" "0.427"
$b[[7]]
[1] "379.68" "1.208"
$b[[8]]
[1] "379.67" "3.949"
$b[[9]]
[1] "379.66" "1.115"
$b[[10]]
[1] "379.65" "7.200"
$b[[11]]
[1] "379.58" "0.308"
$b[[12]]
[1] "379.57" "0.500"
$b[[13]]
[1] "379.55" "1.100"
$b[[14]]
[1] "379.53" "1.000"
$b[[15]]
[1] "379.45" "1.855"
$b[[16]]
[1] "379.41" "5.333"
$b[[17]]
[1] "379.36" "3.149"
$b[[18]]
[1] "379.33" "6.838"
$b[[19]]
[1] "379.32" "10.000"
$b[[20]]
[1] "379.31" "0.806"
$a
$a[[1]]
[1] "380.09" "0.658"
$a[[2]]
[1] "380.10" "0.345"
$a[[3]]
[1] "380.12" "0.490"
$a[[4]]
[1] "380.18" "1.000"
$a[[5]]
[1] "380.20" "1.028"
$a[[6]]
[1] "380.21" "2.476"
$a[[7]]
[1] "380.22" "1.689"
$a[[8]]
[1] "380.28" "0.846"
$a[[9]]
[1] "380.30" "11.570"
$a[[10]]
[1] "380.34" "16.795"
$a[[11]]
[1] "380.35" "7.689"
$a[[12]]
[1] "380.36" "2.984"
$a[[13]]
[1] "380.37" "1.920"
$a[[14]]
[1] "380.38" "5.508"
$a[[15]]
[1] "380.39" "1.565"
$a[[16]]
[1] "380.40" "14.881"
$a[[17]]
[1] "380.41" "19.781"
$a[[18]]
[1] "380.42" "3.557"
$a[[19]]
[1] "380.43" "6.826"
$a[[20]]
[1] "380.44" "13.258"
I have a dataset looks like this
> data.frame("letter" = letters, "words" = paste0(1:26,letters, letters,",", rev(letters),letters,5:26, ",", letters, 1:24, rev(letters)))
letter words
1 a 1aa,za5,a1z
2 b 2bb,yb6,b2y
3 c 3cc,xc7,c3x
4 d 4dd,wd8,d4w
5 e 5ee,ve9,e5v
...
And I would like to turn this table into
[[a]]
[1] "1aa" "za5" "a1z"
[[b]]
[1] "2bb" "yb6" "b2y"
[[c]]
[1] "3cc" "xc7" "c3x"
[[d]]
[1] "4dd" "wd8" "d4w"
[[e]]
[1] "5ee" "ve9" "e5v"
...
I have tried to use a for loop which works for me, however, when the nrow of this dataframe increase, it takes longer time. And I would like to know if there is a cleaner wayt to do so?
Your answer is much appreciated.
Thank you very much!!
The function strsplit is what you are looking for. Try :
df = data.frame("letter" = letters, "words" = paste0(1:26,letters, letters,",", rev(letters),letters,5:26, ",", letters, 1:24, rev(letters)))
strsplit(as.character(df$words),',',fixed= TRUE)
[[1]]
[1] "1aa" "za5" "a1z"
[[2]]
[1] "2bb" "yb6" "b2y"
[[3]]
[1] "3cc" "xc7" "c3x"
[[4]]
[1] "4dd" "wd8" "d4w"
[[5]]
[1] "5ee" "ve9" "e5v"
I'm working on a shiny app which plots data trees. I'm looking to incorporate the shinyTree app to permit quick comparison of plotted nodes. The issue is that the shinyTree app returns a redundant list of lists of the sub node plot.
The actual list of list is included below. I would like to keep the longest branches only. I would also like to remove the id node (integer node), I'm struggling as to why it even shows up based on the list. I have tried many different methods to work with this list but it's been a real struggle. The list concept is difficult to understand.
I create the data.tree and plot via:
dataTree.a <- FromListSimple(checkList)
plot(dataTree.a)
> checkList
[[1]]
[[1]]$Asia
[[1]]$Asia$China
[[1]]$Asia$China$Beijing
[[1]]$Asia$China$Beijing$Round
[[1]]$Asia$China$Beijing$Round$`20383994`
[1] 0
[[2]]
[[2]]$Asia
[[2]]$Asia$China
[[2]]$Asia$China$Beijing
[[2]]$Asia$China$Beijing$Round
[1] 0
[[3]]
[[3]]$Asia
[[3]]$Asia$China
[[3]]$Asia$China$Beijing
[1] 0
[[4]]
[[4]]$Asia
[[4]]$Asia$China
[[4]]$Asia$China$Shanghai
[[4]]$Asia$China$Shanghai$Round
[[4]]$Asia$China$Shanghai$Round$`23740778`
[1] 0
[[5]]
[[5]]$Asia
[[5]]$Asia$China
[[5]]$Asia$China$Shanghai
[[5]]$Asia$China$Shanghai$Round
[1] 0
[[6]]
[[6]]$Asia
[[6]]$Asia$China
[[6]]$Asia$China$Shanghai
[1] 0
[[7]]
[[7]]$Asia
[[7]]$Asia$China
[1] 0
[[8]]
[[8]]$Asia
[[8]]$Asia$India
[[8]]$Asia$India$Delhi
[[8]]$Asia$India$Delhi$Round
[[8]]$Asia$India$Delhi$Round$`25703168`
[1] 0
[[9]]
[[9]]$Asia
[[9]]$Asia$India
[[9]]$Asia$India$Delhi
[[9]]$Asia$India$Delhi$Round
[1] 0
[[10]]
[[10]]$Asia
[[10]]$Asia$India
[[10]]$Asia$India$Delhi
[1] 0
[[11]]
[[11]]$Asia
[[11]]$Asia$India
[1] 0
[[12]]
[[12]]$Asia
[[12]]$Asia$Japan
[[12]]$Asia$Japan$Tokyo
[[12]]$Asia$Japan$Tokyo$Round
[[12]]$Asia$Japan$Tokyo$Round$`38001000`
[1] 0
[[13]]
[[13]]$Asia
[[13]]$Asia$Japan
[[13]]$Asia$Japan$Tokyo
[[13]]$Asia$Japan$Tokyo$Round
[1] 0
[[14]]
[[14]]$Asia
[[14]]$Asia$Japan
[[14]]$Asia$Japan$Tokyo
[1] 0
[[15]]
[[15]]$Asia
[[15]]$Asia$Japan
[1] 0
[[16]]
[[16]]$Asia
[1] 0
Well, I did cobble together a poor hack to make this work here is what I did to the 'checkList' list
checkList <- get_selected(tree, format = "slices")
# Convert and collapse shinyTree slices to data.tree
# This is a bit of a cluge to work the graphic with
# shinyTree an alternate one liner is in works
# This transform works by finding the longest branches
# and only plotting them since the other branches are
# subsets due to the slices.
# Extract the checkList name (as characters) from the checkList
tmp <- names(unlist(checkList))
# Determine the length of the individual checkList Names
lens <- lapply(tmp, function(x) length(strsplit(x, ".", fixed=TRUE)[[1]]))
# Find the elements with the highest length returns a list of high vals
lens.max <- which(lens == max(sapply(lens, max)))
# Replace all '.' with '\' prepping for DataFrameTable Converions
tmp <- relist(str_replace_all(tmp, "\\.", "/"), skeleton=tmp)
# Add a root node to work with multiple branches
tmp <- unlist(lapply(tmp, function(x) paste0("Root/", x)))
# Create a list of only the longest branches
longBranches <- as.list(tmp[lens.max])
# Convert the list into a data.frame for convert
longBranches.df <- data.frame(pathString = do.call(rbind, longBranches))
# Publish the data.frame for use
vals$selDF <- longBranches.df
#save(checkList, file = "chkLists.RData") # Save for troubleshooting
print(vals$selDF)ode here
The new checkList looks like this:
[1] "Root/Europe/France/Paris/Round/10843285" "Root/Europe/France/Paris/Round"
[3] "Root/Europe/France/Paris" "Root/Europe/France"
[5] "Root/Europe/Germany/Berlin/Diamond/3563194" "Root/Europe/Germany/Berlin/Diamond"
[7] "Root/Europe/Germany/Berlin/Round/3563194" "Root/Europe/Germany/Berlin/Round"
[9] "Root/Europe/Germany/Berlin" "Root/Europe/Germany"
[11] "Root/Europe/Italy/Rome/Round/3717956" "Root/Europe/Italy/Rome/Round"
[13] "Root/Europe/Italy/Rome" "Root/Europe/Italy"
[15] "Root/Europe/United Kingdom/London/Round/10313307" "Root/Europe/United Kingdom/London/Round"
[17] "Root/Europe/United Kingdom/London" "Root/Europe/United Kingdom"
[19] "Root/Europe"
It works :)... but I think this could be done with a two liner.... I'll work on it again in a week or so. Any other Ideas would be appreciated.
I have a list like the following in R:
data1<-list("A" = 1, "B" = 2, "C" = 3,"D" = 4)
and when I print data1 I have:
$A
[1] 1
$B
[1] 2
$C
[1] 3
$D
[1] 4
I have a csv file with the values:
alt1,alt2,alt3,alt4
appear,certain,dance,example
apply,danger,chance,excellent
where alt1,alt2,... are the headers of the csv.file
I would like to extract the second row from my csv file so that I could get something like data1, I have done the following:
getData=read.csv("test.csv",header=TRUE)
q<-getData[2,]
print(q)
anylist<-list()
anylist[[q[1]]]<-1
anylist[[q[2]]]<-2
anylist[[q[3]]]<-3
anylist[[q[4]]]<-4
print(anylist)
because I need that anylist to have the same structure like data1, I mean if I will have to write directly it would be:
anylist<-list("apply" = 1, "danger" = 2, "chance" = 3,"excellent" = 4)
so when I print anylist I want to print:
$apply
[1] 1
$danger
[1] 2
$chance
[1] 3
$excellent
[1] 4
but I got the error:
Error in anylist[[q[1]]] <- 1 : invalid subscript type 'list'
The following quick function will take a row of data, determine the order, and associate order and name, outputting a list. I believe this is what you wanted, right? If you wanted to do this for many rows, simply use an apply statement, with MARGIN=1, and you will get a list of lists. Is this what you were looking for?
getNames=function(row){
retList=as.list(order(row))
names(retList) = as.character(sort(row))
return(retList)
}
...here's a quick validation.
test=c("apply", "danger", "chance", "excellent")
getNames(test)
$apply
[1] 1
$chance
[1] 3
$danger
[1] 2
$excellent
[1] 4
test2=c('alt1','alt2','alt3','alt4')
getNames(test2)
$alt1
[1] 1
$alt2
[1] 2
$alt3
[1] 3
$alt4
[1] 4
Gotcha!
getData=read.csv("test.csv",header=TRUE,stringsAsFactors=FALSE)
q<-getData[2,]
n<-as.list(c(1:4))
names(n)<-q
I am in the trouble of getting the values which have the same dates from two different data sources in R. The code is
#Monthly data
month_data <- c(580.11, 618.25, 641.24, 604.85, 580.86, 580.07, 632.97,
685.09, 754.50, 680.30, 698.37, 707.38, 480.11, 528.25,
541.24, 614.85, 680.86)
month_dates <- seq(as.Date("2001/06/01"), by = "1 months", length = 17)
month_data <- data.frame(month_dates, month_data)
#the dates_for_match is a list:
dates_for_match<-list(c( "2001-08-01","2001-09-01", "2001-10-01"),c("2001-11-01","2001-12-01","2002-01-01"),c("2002-02-01","2002-03-01","2002-04-01"),c("2002-05-01","2002-06-01","2002-07-01"),c( "2002-08-01","2002-09-01", "2002-10-01"))
Example:
> dates_for_match
[[1]]
[1] "2001-08-01" "2001-09-01" "2001-10-01"
[[2]]
[1] "2001-11-01" "2001-12-01" "2002-01-01"
[[3]]
[1] "2002-02-01" "2002-03-01" "2002-04-01"
[[4]]
[1] "2002-05-01" "2002-06-01" "2002-07-01"
[[5]]
[1] "2002-08-01" "2002-09-01" "2002-10-01"
I want to use the dates_for_match list to get the values from month_data that have the same dates.
You need %in%...
month_data[ month_dates %in% unlist( dates_for_match ) , 2 ]