Re: [R] Quadratic programming

2020-09-22 Thread Abby Spurdle
> I'm trying to replicate a C++ code with R. Notes: (1) I'd recommend you make the code more modular. i.e. One function for initial data prep/modelling, one function for setting up and solving the QP, etc. This should be easier to debug. (However, you would probably have to do it to the C++ code f

Re: [R] Split

2020-09-22 Thread Bert Gunter
That was still slower and doesn't quite give what was requested: > cbind(F1,utils::strcapture("([^_]*)_(.*)", F1$text, proto=data.frame(Before_=character(), After_=character( ID1 ID2 text Before_ After_ 1 A1 B1 NONE 2 A1 B1 cf_12 cf 12 3 A1 B1 NONE 4 A2 B2 X

Re: [R] Split

2020-09-22 Thread Bill Dunlap
Another way to make columns out of the stuff before and after the underscore, with NAs if there is no underscore, is utils::strcapture("([^_]*)_(.*)", F1$text, proto=data.frame(Before_=character(), After_=character())) -Bill On Tue, Sep 22, 2020 at 4:25 PM Bert Gunter wrote: > To be clear, I t

Re: [R] Split

2020-09-22 Thread Bert Gunter
Oh, if efficiency is a consideration, then my code is about 15 times as fast as Rui's: > F2 <- F1[rep(1:5,1e6),] ## 5 million rows ##Rui's > system.time({ + F2$Y1 <- +grepl("_", F2$text) + tmp <- strsplit(as.character(F2$text), "_") + tmp <- lapply(tmp, function(x) if(length(x) == 1) c

Re: [R] Split

2020-09-22 Thread Val
Thank you all for the help! LMH, Yes I would like to see the alternative. I am using this for a large data set and if the alternative is more efficient than this then I would be happy. On Tue, Sep 22, 2020 at 6:25 PM Bert Gunter wrote: > > To be clear, I think Rui's solution is perfectly fine

Re: [R] Split

2020-09-22 Thread Bert Gunter
To be clear, I think Rui's solution is perfectly fine and probably better than what I offer below. But just for fun, I wanted to do it without the lapply(). Here is one way. I think my comments suffice to explain. > ## which are the non "_" indices? > wh <- grep("_",F1$text, fixed = TRUE, invert

Re: [R] Help with the Error Message in R "Error in 1:nchid : result would be too long a vector"

2020-09-22 Thread Rui Barradas
Hello, Please keep this on the list so that others can give their contribution. If you have reshaped your data can you post the code you ran to reshape it? Right now we only have the original attachment, in wide format, not the long format data. Rui Barradas Às 21:55 de 22/09/20, Rahul Chak

Re: [R] Split

2020-09-22 Thread LMH
Sometimes it just makes more sense to pre-process your data and get it into the format you need. It just depends on whether you are more comfortable programing in R or in some other text manipulation language like bash/sed/awk/grep etc. If you know how to do this with other tools, you could writ

Re: [R] Split

2020-09-22 Thread Rui Barradas
Hello, A base R solution with strsplit, like in your code. F1$Y1 <- +grepl("_", F1$text) tmp <- strsplit(as.character(F1$text), "_") tmp <- lapply(tmp, function(x) if(length(x) == 1) c(x, ".") else x) tmp <- do.call(rbind, tmp) colnames(tmp) <- c("X1", "X2") F1 <- cbind(F1[-3], tmp)# remove

Re: [R] Split

2020-09-22 Thread Rui Barradas
Hello, Something like this? F1$Y1 <- +grepl("_", F1$text) F1 <- F1[c(1, 2, 4, 3)] F1 <- tidyr::separate(F1, text, into = c("X1", "X2"), sep = "_", fill = "right") F1 Hope this helps, Rui Barradas Às 19:55 de 22/09/20, Val escreveu: HI All, I am trying to create new columns based on an

[R] Split

2020-09-22 Thread Val
HI All, I am trying to create new columns based on another column string content. First I want to identify rows that contain a particular string. If it contains, I want to split the string and create two variables. Here is my sample of data. F1<-read.table(text="ID1 ID2 text A1 B1 NONE A1

Re: [R] Help with the Error Message in R "Error in 1:nchid : result would be too long a vector"

2020-09-22 Thread Rahul Chakraborty
David, My apologies with the first one. I was checking different tutorials on mlogit where they were using mlogit.data, so I ended up using it. I am not getting what you are saying by the "duplicates in first two columns". See, my first column is IND which identifies my individuals, second column

Re: [R] Help with the Error Message in R "Error in 1:nchid : result would be too long a vector"

2020-09-22 Thread Rui Barradas
Hello, I apologize if the rest of quotes prior to David's email are missing, for some reason today my mail client is not including them. As for the question, there are two other problems: 1) Alt_name is misspelled, it should be ALT_name; 2) the data is in wide, not long, format. A 3rd, prob

Re: [R] text on curve

2020-09-22 Thread Berry, Charles
> On Sep 22, 2020, at 1:10 AM, Jinsong Zhao wrote: > > Hi there, > > I write a simple function that could place text along a curve. Since I am not > familiar with the operation of rotating graphical elements, e.g., text, > rectangle, etc., I hope you could give suggestions or hints on how t

Re: [R] Help with the Error Message in R "Error in 1:nchid : result would be too long a vector"

2020-09-22 Thread David Winsemius
You were told two things about your code: 1) mlogit.data is deprecated by the package authors, so use dfidx. 2) dfidx does not allow duplicate ids in the first two columns. Which one of those are you asserting is not accurate? -- David. On 9/21/20 10:20 PM, Rahul Chakraborty wrote: Hello

Re: [R] aggregate semi-hourly data not 00-24 but 9-9

2020-09-22 Thread Stefano Sofia
Yes, thank you so much. Stefano (oo) --oOO--( )--OOo Stefano Sofia PhD Civil Protection - Marche Region Meteo Section Snow Section Via del Colle Ameno 5 60126 Torrette di Ancona, Ancona Uff: 071 806 7743 E-mail: stefano.so...@regione.marche.it ---Oo-oO

Re: [R] Quadratic programming

2020-09-22 Thread Maija Sirkjärvi
I really appreciate you helping me with this! I just don't seem to figure it out. (1) I don't know why you think bvec should be a matrix. The documentation clearly says it should be a vector (implying not a matrix). - I've written it in a form of a matrix with one row and 2*J-3 columns. (0,2*J-3,

Re: [R] aggregate semi-hourly data not 00-24 but 9-9

2020-09-22 Thread Eric Berger
Thanks Jeff. Stefano, per Jeff's comment, you can replace the line df1$data_POSIXminus9 <- df1$data_POSIX - lubridate::hours(9) by df1$data_POSIXminus9 <- df1$data_POSIX - as.difftime(9,units="hours") On Mon, Sep 21, 2020 at 8:06 PM Jeff Newmiller wrote: > > The base R as.difftime function is

Re: [R] text on curve

2020-09-22 Thread Jim Lemon
Hi Jinsong, This is similar to the "arctext" function in plotrix. I don't want to do all the trig right now, but I would suggest placing the characters on the curve and then offsetting them a constant amount at right angles to the slope of the curve at each letter. I would first try having a "minsp

[R] text on curve

2020-09-22 Thread Jinsong Zhao
Hi there, I write a simple function that could place text along a curve. Since I am not familiar with the operation of rotating graphical elements, e.g., text, rectangle, etc., I hope you could give suggestions or hints on how to improve it. Thanks in advance. # Here is the code: getCurrentAs