Posts

r - Warning when downcasting in Rcpp? -

i have rcpp function should take integervector input (as toint ). want use on vector of integers, on vector of doubles integers (e.g. 1:4 of type integer 1:4 + 1 of type double ). yet, when used on real floating point numbers (e.g. 1.5 ), return warning or error instead of silently rounding values (to make them integers). #include <rcpp.h> using namespace rcpp; // [[rcpp::export]] integervector toint(robject x) { return as<integervector>(x); } > toint(c(1.5, 2.4)) # warning [1] 1 2 > toint(1:2 + 1) # no need of warning [1] 2 3 rcpp sugar has need. here 1 possible implementation: #include <rcpp.h> using namespace rcpp; // [[rcpp::export]] integervector fprive(const robject & x) { numericvector nv(x); integervector iv(x); if (is_true(any(nv != numericvector(iv)))) warning("uh-oh"); return(iv); } /*** r fprive(c(1.5, 2)) fprive(c(1l, 2l)) */ its output follows: r> rcpp::sourcecpp('/tmp/f...

java - Add HttpCookie to CookieStore -

i need add few cookies authorize on website. cookies added successfully, missing when making request: import java.io.*; import java.net.*; public class main { static public void main(string[] args) throws exception { cookiemanager cookiemanager = new cookiemanager(null, cookiepolicy.accept_all); cookiestore cookiejar = cookiemanager.getcookiestore(); cookiehandler.setdefault(cookiemanager); httpcookie cookie = new httpcookie("name123", "value123"); cookiejar.add(new uri("http://httpbin.org"), cookie); httpurlconnection connection = (httpurlconnection) new url("http://httpbin.org/cookies").openconnection(); connection.setrequestmethod("get"); connection.connect(); bufferedreader in; stringbuilder response = new stringbuilder(); string inputline; in = new bufferedreader(new inputstreamreader(connection.getinputstream())); ...

r - conditional sum with dates in column names -

Image
want calculate conditional sum based on specified dates in r. sample df start_date = c("7/24/2017", "7/1/2017", "7/25/2017") end_date = c("7/27/2017", "7/4/2017", "7/28/2017") `7/23/2017` = c(1,5,1) `7/24/2017` = c(2,0,2) `7/25/2017` = c(0,0,10) `7/26/2017` = c(2,2,2) `7/27/2017` = c(0,0,0) df = data.frame(start_date,end_date,`7/23/2017`,`7/24/2017`,`7/25/2017`,`7/26/2017`,`7/27/2017`) in excel looks like: i want perform calculations specified in column h conditional sum of columns c through g based on dates specified in columns , b . apparently, excel allows columns dates not r. you achieve follows: # number of trailing columns without numeric values c = 2 # create separate vector dates dates = as.date(gsub("x","",tail(colnames(df),-c)),format="%m.%d.%y") # convert date columns in dataframe df$start_date = as.date(df$start_date,format="%m/%d/%y") df$end_d...

r - Panel data models: Duplicate couples error when there are no duplicate couples -

i have panel data set individual (dyad_id), integer, , time (year_month) date variable. try running following code: df.fe <- plm(deaths_civilians ~ deaths_a_lag + deaths_b_lag, data = rebel, index = c("dyad_id", "year_month"), model = "within", effect = "individual") but keep getting following error message: error in pdim.default(index[[1]], index[[2]]) : duplicate couples (id-time) in addition: warning messages: 1: in pdata.frame(data, index) : duplicate couples (id-time) in resulting pdata.frame find out which, use e.g. table(index(your_pdataframe), usena = "ifany") 2: in is.pbalanced.default(index[[1]], index[[2]]) : duplicate couples (id-time) 3: in is.pbalanced.default(index[[1]], index[[2]]) : duplicate couples (id-time) all previous answers question because have more 1 observation same id same time per...

python - IP address must be specified? -

heres code running in python 2.7 on windows 10 import os open('test.txt') f: line in f: response = os.system("ping -c 1" + line) if response == 0: print(line, "is up!") else: print(line, "is down!") the test.txt file contains random ip's, code runs when give out message the ip address must specified. problem don't know how within script. when use regular command promt , ping -c 1 google.com runs through reading text file above python script same google.com needs specified. q1: mean have ip specified , how it? q2: should write code in diffrent manner importing different module? import os open('test.txt') f: line in f: response = os.system("ping -c 1 " + line.strip()) if response == 0: print(line, "is up!") else: print(line, "is down!") strip newline off end of records file , add space in ping comm...

javascript - Return variable by socket client -

i have following question. how can return new value sockets, js file? app.js io.on('connection', function (socket) { socket.currentroomid = 0; socket.currentroomname = "loading..."; socket.currentroomowner = 1; socket.currentroomentry = 1.1; }); roommanager.js adapter.query(sql, function(err, rows) { rows.foreach( (row) => { socket.currentroomid = parseint(row.id); socket.currentroomname = row.room_name; socket.currentroomowner = row.room_owner; socket.currentroomentry = row.room_entry; }); }); greetings. you have emit , listen this, roommanager.js adapter.query(sql, function(err, rows) { rows.foreach( (row) => { // emit setcurrentdata row data socket.emit('setcurrentdata', { data: row }); }); }); app.js io.on('connection', function (socket) { socket.currentroomid = 0; socket.currentroomname = "loading..."; socket.cur...

r - Value label for variable -

i want attach value label variables (0 = "male" , 1 = "female") in data frame. i tried this: cb$gender <- factor(cb$gender, levels = c(0,1), labels = c("male", "female")) but result: [1] female male male male female female male female female male male male male male [15] male female female male female female male male male female male female male male [29] male female female male any idea? i don't quite know wrong splitting apart seemed work gender<-c("male","female","male","female") gender <- as.factor(gender) levels(gender)<-c(0,1) of course, pay attention fact used own made data. so try subset data first gender<-cb$gender and apply above. best of luck!