Re: [R] Create new data frame with conditional sums

2023-10-24 Thread peter dalgaard
This seems to work. A couple of fine points, including handling duplicated Pct values right, which is easier if you do the reversed cumsum. > dd2 <- dummydata[order(dummydata$Pct),] > dd2$Cum <- rev(cumsum(rev(dd2$Totpop))) > use <- !duplicated(dd2$Pct) > approx(dd2$Pct[use], dd2$Cum[use], ctof,

Re: [R] Create new data frame with conditional sums

2023-10-16 Thread Bert Gunter
Sorry, misstatements. It should (of course) read: If one makes the reasonable assumption that Pct is much larger than Cutoff, sorting Pct is the expensive part e.g O(nlog2(n) for Quicksort (n = length Pct). I believe looping is O(n^2). etc. On Mon, Oct 16, 2023 at 7:48 AM Bert Gunter wrote: > >

Re: [R] Create new data frame with conditional sums

2023-10-16 Thread Bert Gunter
If one makes the reasonable assumption that Pct is much larger than Cutoff, sorting Cutoff is the expensive part e.g O(nlog2(n) for Quicksort (n = length Cutoff). I believe looping is O(n^2). Jeff's approach using findInterval may be faster. Of course implementation details matter. -- Bert On Mo

Re: [R] Create new data frame with conditional sums

2023-10-16 Thread Leonard Mada via R-help
Dear Jason, The code could look something like: dummyData = data.frame(Tract=seq(1, 10, by=1),     Pct = c(0.05,0.03,0.01,0.12,0.21,0.04,0.07,0.09,0.06,0.03),     Totpop = c(4000,3500,4500,4100,3900,4250,5100,4700,4950,4800)) # Define the cutoffs # - allow for duplicate entries; by = 0.03; # by

Re: [R] Create new data frame with conditional sums

2023-10-15 Thread Leonard Mada via R-help
Dear Jason, I do not think that the solution based on aggregate offered by GPT was correct. That quasi-solution only aggregates for every individual level. As I understand, you want the cumulative sum. The idea was proposed by Bert; you need only to sort first based on the cutoff (e.g. usin

Re: [R] Create new data frame with conditional sums

2023-10-15 Thread Jason Stout, M.D.
the result result So thanks to all for considering this query�we're in a brave new world of AI-generated coding. Message: 3 Date: Fri, 13 Oct 2023 20:13:56 + From: "Jason Stout, M.D." To: "r-help@r-project.org" Subject: [R] Create new data frame with conditi

Re: [R] Create new data frame with conditional sums

2023-10-14 Thread Jeff Newmiller via R-help
Pre-compute the per-interval answers and use findInterval to look up the per-row answers... dat <- read.table( text= "Tract Pct Totpop 1 0.054000 2 0.033500 3 0.014500 4 0.124100 5 0.

Re: [R] Create new data frame with conditional sums

2023-10-14 Thread Bert Gunter
Well, here's one way to do it: (dat is your example data frame) Cutoff <- seq(0, .15, .01) Pop <- with(dat, sapply(Cutoff, \(p)sum(Totpop[Pct >= p]))) I think there must be a more efficient way to do it with cumsum(), though. Cheers, Bert On Sat, Oct 14, 2023 at 12:53 AM Jason Stout, M.D. wrot

[R] Create new data frame with conditional sums

2023-10-14 Thread Jason Stout, M.D.
This seems like it should be simple but I can't get it to work properly. I'm starting with a data frame like this: Tract Pct Totpop 1 0.054000 2 0.033500 3 0.014500 4 0.124100 5 0.21

Re: [R] create new

2017-03-25 Thread Jim Lemon
Hi Val, How about this: cinmat<- matrix(c(mydat$x1>0,mydat$x1==0&mydat$x2==0,mydat$x1==0&mydat$x2>0), ncol=3) mydat$x4<-rowSums(mydat[,c("x1","x3","x2")]*cinmat) Jim On Sat, Mar 25, 2017 at 2:56 PM, Val wrote: > Hi all, > > > I have several variables in a group and one group contains three

Re: [R] create new

2017-03-24 Thread Bert Gunter
?ifelse Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Mar 24, 2017 at 8:56 PM, Val wrote: > Hi all, > > > I have several variables in a g

[R] create new

2017-03-24 Thread Val
Hi all, I have several variables in a group and one group contains three variables. Sample of data ( Year, x1, x3 and x2) mydat <- read.table(header=TRUE, text=' Year x1 x3 x2 Year1 10 120 Year2 0 150 Year3 0 020 Year4 25 0 12 Year5 15 25 12 Year6 0 16 14 Y

Re: [R] create new matrices with specific patterns

2015-01-27 Thread JSHuang3181
Hi, It appears that you want B to be rows 1 thru 4, 6 thru 9, 11 thru 14 and 16 thru 17 from A. >B <- A[c(1:4, 6:9, 11:14, 16:17),] >E <- A[16:19,] -- View this message in context: http://r.789695.n4.nabble.com/create-new-matrices-with-specific-patterns-tp4702350p4702354.html Sent from the R

Re: [R] create new matrices with specific patterns

2015-01-27 Thread Jim Lemon
Hi Kathryn, Selecting the elements of matrix A as you describe is not too difficult: select_A<-function(A,B) { thisA<-A[A[,1] == B$p[1],] newmat<-matrix(c(rep(p[1],B$nq[1]),rep(thisA[,2],length.out=B$nq[1])),ncol=2) for(i in 2:dim(B)[1]) { thisA<-A[A[,1] == B$p[i],] newmat<- rbind(newmat

Re: [R] create new matrices with specific patterns

2015-01-26 Thread Kathryn Lord
Sorry for inconvenience. For clarification, The matrix B looks like > B P Q [1,] 1 1 [2,] 1 2 [3,] 1 3 [4,] 1 4 [5,] 2 1 [6,] 2 2 [7,] 2 3 [8,] 2 4 [9,] 3 1 [10,] 3 2 [11,] 3 3 [12,] 3 4 [13,] 4 1 [14,] 4 2 The matrix E is > E P Q [1,] 4 1 [2,] 4 2 [3,] 4 3 [4,] 4 4 On Tu

[R] create new matrices with specific patterns

2015-01-26 Thread Kathryn Lord
Dear R users, Suppose I have a matrix A. > p <- 1:4 > q <- 1:5 > P<-rep(p, each=5) > Q<-rep(q, 4) > > A <- cbind(P,Q) > A P Q [1,] 1 1 [2,] 1 2 [3,] 1 3 [4,] 1 4 [5,] 1 5 [6,] 2 1 [7,] 2 2 [8,] 2 3 [9,] 2 4 [10,] 2 5 [11,] 3 1 [12,] 3 2 [13,] 3 3 [14,] 3 4 [15,] 3 5 [16,] 4 1 [17,]

Re: [R] create new column by replacing multiple unique values in existing column

2014-09-11 Thread John McKown
On Thu, Sep 11, 2014 at 10:49 AM, raz wrote: > Hi, > > I got the following data frame: > dat1 <- read.table(text="a,b > 1,A1 > 2,A1 > 3,A1 > 4,A1 > 5,A1 > 6,A2 > 7,A2 > 8,A2 > 9,A2 > 10,A2 > 11,B1 > 12,B1 > 13,B1 > 14,B1 > 15,B1",sep=",",header=T) > > > I would like to add a new column dat1$new b

Re: [R] create new column by replacing multiple unique values in existing column

2014-09-11 Thread David L Carlson
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of raz Sent: Thursday, September 11, 2014 10:49 AM To: r-help@r-project.org Subject: [R] create new column by replacing multiple unique values in existing column Hi, ​I got the following d

[R] create new column by replacing multiple unique values in existing column

2014-09-11 Thread raz
Hi, ​I got the following data frame: dat1 <- read.table(text="a,b 1,A1 2,A1 3,A1 4,A1 5,A1 6,A2 7,A2 8,A2 9,A2 10,A2 11,B1 12,B1 13,B1 14,B1 15,B1",sep=",",header=T) ​ ​I would like to add a new column dat1$new based on column "b" (dat$b) in which values will be substituted according to their un

Re: [R] create new rows with 0, if nessecary

2014-05-16 Thread arun
d Carlson -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Martyn Byng Sent: Friday, May 16, 2014 6:51 AM To: Mat; r-help@r-project.org Subject: Re: [R] create new rows with 0, if nessecary Hi, Something like: aa <-structure(list(Cat

Re: [R] create new rows with 0, if nessecary

2014-05-16 Thread David L Carlson
oject.org [mailto:r-help-boun...@r-project.org] On Behalf Of Martyn Byng Sent: Friday, May 16, 2014 6:51 AM To: Mat; r-help@r-project.org Subject: Re: [R] create new rows with 0, if nessecary Hi, Something like: aa <-structure(list(Cat = c(101, 103), Hours = c(15, 10)), .Names = c("Cat",&

Re: [R] create new rows with 0, if nessecary

2014-05-16 Thread Martyn Byng
Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Mat Sent: 16 May 2014 12:18 To: r-help@r-project.org Subject: [R] create new rows with 0, if nessecary hello togehter, i have a little problem, i have frames like this two: Cat Hours

[R] create new rows with 0, if nessecary

2014-05-16 Thread Mat
hello togehter, i have a little problem, i have frames like this two: Cat Hours A101 15 B103 10 Cat Hours A103 16 B106 11 I need to convert this 2 data.frames into the following equal structure: Cat Hours A101 B

Re: [R] create new column to combine 2 data.frames

2013-08-30 Thread arun
IKAL Petr To: Mat ; "r-help@r-project.org" Cc: Sent: Friday, August 30, 2013 5:57 AM Subject: Re: [R] create new column to combine 2 data.frames Hi Sorry, I did not read your question to the end library(reshape) merge(dat1,cast(melt(dat2, c("ID", "Type")),

Re: [R] create new column to combine 2 data.frames

2013-08-30 Thread Mat
thanks. Works perfectly you made my day :-) -- View this message in context: http://r.789695.n4.nabble.com/create-new-column-to-combine-2-data-frames-tp4674963p4674994.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-projec

Re: [R] create new column to combine 2 data.frames

2013-08-30 Thread PIKAL Petr
ly change NA to 0 if you want. Regards Petr > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of Mat > Sent: Friday, August 30, 2013 11:27 AM > To: r-help@r-project.org > Subject: Re: [R] create new column to c

Re: [R] create new column to combine 2 data.frames

2013-08-30 Thread PIKAL Petr
Hi see ?merge something like merge(df1, df2) Petr > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of Mat > Sent: Friday, August 30, 2013 10:37 AM > To: r-help@r-project.org > Subject: [R] create new c

Re: [R] create new column to combine 2 data.frames

2013-08-30 Thread Rui Barradas
Hello, Ok, try instead library(reshape2) tmp <- dcast(data = dat2, ID ~ Type, value.var = "Type") tmp[-1] <- lapply(tmp[-1], function(x){ y <- integer(length(x)) s <- as.character(x[!is.na(x)])[1] idx <- which(as.character(dat2[["Type"]]) == s) y[!is.na(x)] <-

Re: [R] create new column to combine 2 data.frames

2013-08-30 Thread Mat
Thanks first. that doesn't look bad. But if have a equal ID in dat2, the days are no longer correct. the correct data.frame has to look like this one: ID Name Management Training 1 1 Jack 13 2 2 John 10 3 3 Jill 04 not this one: ID Name

Re: [R] create new column to combine 2 data.frames

2013-08-30 Thread Rui Barradas
Hello, Suposing that your data frames are named dat1 and dat2, the following works, but it's a bit complicated, maybe there are simpler solutions. dat1 <- read.table(text = " ID Name 1 Jack 2 John 3 Jill ", header = TRUE, stringsAsFactors = FALSE) dat2 <- read.table(text = " ID Days Ty

[R] create new column to combine 2 data.frames

2013-08-30 Thread Mat
Hello together i have a little problem with the combine of two data.frames. I have 2 data.frames, which look like this one: first dataframe: ID Name 1 Jack 2 John 3 Jill second dataframe ID Days Type 13 Training 21 Management 34 Training At the end i want to

Re: [R] Create new records based on event dates in a data frame

2013-08-19 Thread gavinr
Have used the suggested solution (the second method, as it seems a bit leaner) on my real data, and it works perfectly. Thanks. -- View this message in context: http://r.789695.n4.nabble.com/Create-new-records-based-on-event-dates-in-a-data-frame-tp4673839p4674082.html Sent from the R help mail

Re: [R] Create new records based on event dates in a data frame

2013-08-15 Thread arun
Message - From: arun To: Gavin Rudge Cc: R help Sent: Thursday, August 15, 2013 10:10 AM Subject: Re: [R] Create new records based on event dates in a data frame Hi,One way would be: df<- data.frame(case,obsdate=as.Date(obsdate,format="%d/%m/%Y"),score,stringsAsFactors=F

Re: [R] Create new records based on event dates in a data frame

2013-08-15 Thread arun
#5    b 2005-11-10 2011-03-31    79 #6    c 2001-04-01 2011-03-31    65 A.K. - Original Message - From: Gavin Rudge To: "'r-help@r-project.org'" Cc: Sent: Thursday, August 15, 2013 9:22 AM Subject: [R] Create new records based on event dates in a data frame One of t

[R] Create new records based on event dates in a data frame

2013-08-15 Thread Gavin Rudge
One of those simple tasks, but I can't get to first base with it. I've got a data set of observations of subjects over a 10 year period beginning on 1st April 2001 and ending on 31st March 2011. One of may variables is a score based on an intervention on a given date. Before the intervention t

Re: [R] create new matrix from user-defined function

2013-07-15 Thread bcrombie
Arun, thanks for clearing that up. I was making assumptions about R that I shouldn’t have when it comes to column(variable) assignments. Hope everyone has a good Monday. BNC From: arun kirshna [via R] [mailto:ml-node+s789695n4671473...@n4.nabble.com] Sent: Friday, July 12, 2013 11:59 PM To:

Re: [R] create new matrix from user-defined function

2013-07-12 Thread arun
  9   5   6 112 with(dat3,7) #[1] 7  with(dat3,`7`) #Error in eval(expr, envir, enclos) : object '7' not found A.K. - Original Message - From: bcrombie To: r-help@r-project.org Cc: Sent: Friday, July 12, 2013 4:45 PM Subject: Re: [R] create new matrix from user-defined function

Re: [R] create new matrix from user-defined function

2013-07-12 Thread bcrombie
AK, I decided to convert your “with” statement back to index-by-number, and I did look up the ?with help info, but I’m confused about my replacement code below. I got the wrong answer (R didn’t apply the function to my column 1 variable “A_CaseID”). What am I doing wrong? Do I nee

Re: [R] create new matrix from user-defined function

2013-07-12 Thread bcrombie
- From: "Crombie, Burnette N" <[hidden email]> To: arun <[hidden email]> Cc: R help <[hidden email]> Sent: Thursday, July 11, 2013 4:40 PM Subject: RE: [R] create new matrix from user-defined function You understood me perfectly, and I agree is it easier to index usi

Re: [R] create new matrix from user-defined function

2013-07-11 Thread arun
ombie, Burnette N" To: arun Cc: R help Sent: Thursday, July 11, 2013 4:40 PM Subject: RE: [R] create new matrix from user-defined function You understood me perfectly, and I agree is it easier to index using numbers than names.  I'm just afraid if my dataset gets too big I'll mess up

Re: [R] create new matrix from user-defined function

2013-07-11 Thread Crombie, Burnette N
Oh, also thanks for the speed comparisons. Missed that in my first read-through. Very interesting and informative. BNC -Original Message- From: Crombie, Burnette N Sent: Thursday, July 11, 2013 4:40 PM To: 'arun' Cc: R help Subject: RE: [R] create new matrix from us

Re: [R] create new matrix from user-defined function

2013-07-11 Thread arun
t(dtTest[D!=B+C],select=1))  # user  system elapsed  # 0.508   0.000   0.477 identical(res1,res2) #[1] TRUE setnames(res3,"A","MW_EEsDue_ERRORS")  identical(res1,as.data.frame(res3)) #[1] TRUE A.K. - Original Message - From: bcrombie To: r-help@r-project.org Cc: Sent: T

Re: [R] create new matrix from user-defined function

2013-07-11 Thread bcrombie
Wednesday, July 10, 2013 12:19 PM > To: [hidden email] > Subject: [R] create new matrix from user-defined function > > #Let's say I have the following data set: > > dat3 = data.frame(A_CaseID = c(1881, 1882, 1883, 1884, 1885), > B_MW_EEsDue1 = c(2

Re: [R] create new matrix from user-defined function

2013-07-10 Thread Nordlund, Dan (DSHS/RDA)
> -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of bcrombie > Sent: Wednesday, July 10, 2013 12:19 PM > To: r-help@r-project.org > Subject: [R] create new matrix from user-defined function > > #Let&#x

[R] create new matrix from user-defined function

2013-07-10 Thread bcrombie
#Let's say I have the following data set: dat3 = data.frame(A_CaseID = c(1881, 1882, 1883, 1884, 1885), B_MW_EEsDue1 = c(2, 2, 1, 4, 6), C_MW_EEsDue2 = c(5, 5, 4, 1, 6), D_MW_EEsDueTotal = c(7, 9, 5, 6, 112)) dat3 # A_CaseID B_MW_EEsDue1 C_MW_E

Re: [R] create new matrix from user-defined function

2013-07-10 Thread arun
Hi, You could try:   mat1<-matrix(dat3[rowSums(dat3[,2:3])!=dat3[,4],1],ncol=1,dimnames=list(NULL,"MW_EEsDue_ERRORS"))  mat1 # MW_EEsDue_ERRORS #[1,] 1882 #[2,] 1884 #[3,] 1885 A.K. #Let's say I have the following data set: dat3 = data.frame(A_CaseID = c(

Re: [R] Create New Column Inside Data Frame for Many Data Frames

2013-04-14 Thread arun
e - From: "Sparks, John James" To: r-help@r-project.org Cc: Sent: Sunday, April 14, 2013 1:19 PM Subject: [R] Create New Column Inside Data Frame for Many Data Frames Dear R Helpers, I have a large number of data frames and I need to create a new column inside each data frame.  Be

Re: [R] Create New Column Inside Data Frame for Many Data Frames

2013-04-14 Thread Jeff Newmiller
I suggest you read the section on indexing in the Introduction to R document that comes with R. In particular, look at the [[i]] notation. This comes in handy in a couple of ways. First, you shouldn't be working with "many" data frames at once that are stored as separately-named objects. If you

Re: [R] Create New Column Inside Data Frame for Many Data Frames

2013-04-14 Thread Rui Barradas
Hello, I'm not completely sure I've understood. Your variable 'letters' iholds the names of the data.frames? If so it's better if you put yoyr data.frames in a list and then use that list. Something like lst <- list(A, B) for (i in seq_along(lst)){ lst[[i]][["Rate"]] <- ROC(lst[[i]]

[R] Create New Column Inside Data Frame for Many Data Frames

2013-04-14 Thread Sparks, John James
Dear R Helpers, I have a large number of data frames and I need to create a new column inside each data frame. Because there is a large number, I need to "loop" through this, but I don't know the syntax of assigning a new column name dynamically. Below is a simple example of what I need to do.

Re: [R] create new column in a DF according to values from another column

2012-09-26 Thread arun
#7  14    7  V2 #8  17    8  V3 #9  20    9  V2 #10 19   10  V3 A.K. - Original Message - From: jeff6868 To: r-help@r-project.org Cc: Sent: Wednesday, September 26, 2012 6:49 AM Subject: [R] create new column in a DF according to values from another colu

Re: [R] create new column in a DF according to values from another column

2012-09-26 Thread Rui Barradas
Hello, 'if' is not vectorized, it only uses the first value of the multiple condition. 'ifelse' is vectorized and in your case use nested ifelses. DF <- data.frame(number=c(1,4,7,3,11,16,14,17,20,19),data=c(1:10)) v1 <- c(1,7,11,16) v2 <- c(4,14,20) v3 <- c(3,17,19) DF$Station <- ifelse(DF$n

Re: [R] create new column in a DF according to values from another column

2012-09-26 Thread jeff6868
Yes this is it! Thank you for your help Berend! -- View this message in context: http://r.789695.n4.nabble.com/create-new-column-in-a-DF-according-to-values-from-another-column-tp4644217p4644225.html Sent from the R help mailing list archive at Nabble.com.

Re: [R] create new column in a DF according to values from another column

2012-09-26 Thread jim holtman
Here is another technique to use, especially if you have a long list of replacement values: > DF <- data.frame(number=c(1,4,7,3,11,16,14,17,20,19),data=c(1:10)) > > # create a list of replacement values; if you have a lot and > # you can create them automagically, then it is easier > replace <- l

Re: [R] create new column in a DF according to values from another column

2012-09-26 Thread Berend Hasselman
On 26-09-2012, at 12:49, jeff6868 wrote: > Hi everyone, > > I have a small problem in my R-code. > > Imagine this DF for example: > > DF <- data.frame(number=c(1,4,7,3,11,16,14,17,20,19),data=c(1:10)) > > I would like to add a new column "Station" in this DF. This new column must > be automa

[R] create new column in a DF according to values from another column

2012-09-26 Thread jeff6868
Hi everyone, I have a small problem in my R-code. Imagine this DF for example: DF <- data.frame(number=c(1,4,7,3,11,16,14,17,20,19),data=c(1:10)) I would like to add a new column "Station" in this DF. This new column must be automatically filled with: "V1" or "V2" or "V3". The choice must be do

Re: [R] create new variable with ifelse? (reproducible example)

2012-09-16 Thread Niklas Fischer
Best, >> Steve >> >> -- >> >> Message: 25 >> Date: Sat, 15 Sep 2012 23:36:49 +0300 >> From: Niklas Fischer >> To: r-help@r-project.org >> Subject: [R] create new variable with ifelse? (reproducible example) >> Mes

Re: [R] create new variable with ifelse? (reproducible example)

2012-09-16 Thread Rui Barradas
,5),]$clo <- 1 Best, Steve -- Message: 25 Date: Sat, 15 Sep 2012 23:36:49 +0300 From: Niklas Fischer To: r-help@r-project.org Subject: [R] create new variable with ifelse? (reproducible example) Message-ID: Content-Type: text/plain Dear R users, I have a

Re: [R] create new variable with ifelse? (reproducible example)

2012-09-16 Thread Stephen Politzer-Ahles
talong %in% c(4,5),]$clo <- 1 Best, Steve -- Message: 25 Date: Sat, 15 Sep 2012 23:36:49 +0300 From: Niklas Fischer To: r-help@r-project.org Subject: [R] create new variable with ifelse? (reproducible example) Message-ID: Content-Type: text/plain Dear R

Re: [R] create new variable with ifelse? (reproducible example)

2012-09-16 Thread Niklas Fischer
Thank you very much for very valuable comments. They are very informative. Bests, Niklas 2012/9/16 Ted Harding > [See at end] > On 15-Sep-2012 20:36:49 Niklas Fischer wrote: > > Dear R users, > > > > I have a reproducible data and try to create new variable "clo" is 1 if > > know variable is

Re: [R] create new variable with ifelse? (reproducible example)

2012-09-15 Thread arun
  5   1 #10067_a1 10067_a2   very well    5   1 #10076_a1 10076_a2 fairly well    5   1 A.K. ----- Original Message - From: Niklas Fischer To: r-help@r-project.org Cc: Sent: Saturday, September 15, 2012 4:36 PM Subject: [R] create new variable with ifelse? (re

Re: [R] create new variable with ifelse? (reproducible example)

2012-09-15 Thread Ted Harding
[See at end] On 15-Sep-2012 20:36:49 Niklas Fischer wrote: > Dear R users, > > I have a reproducible data and try to create new variable "clo" is 1 if > know variable is equal to "very well" or "fairly well" and getalong is 4 or > 5 > otherwise it is 0. >[A] rep_data<- read.table(header=TRUE, te

Re: [R] create new variable with ifelse? (reproducible example)

2012-09-15 Thread Milan Bouchet-Valat
Le samedi 15 septembre 2012 à 23:36 +0300, Niklas Fischer a écrit : > Dear R users, > > I have a reproducible data and try to create new variable "clo" is 1 if > know variable is equal to "very well" or "fairly well" and getalong is 4 or > 5 > otherwise it is 0. > > rep_data<- read.table(header=

[R] create new variable with ifelse? (reproducible example)

2012-09-15 Thread Niklas Fischer
Dear R users, I have a reproducible data and try to create new variable "clo" is 1 if know variable is equal to "very well" or "fairly well" and getalong is 4 or 5 otherwise it is 0. rep_data<- read.table(header=TRUE, text=" id1id2know getalong 10016_a1 1001

Re: [R] Create new Vector based on two colums

2012-04-25 Thread ilai
On Wed, Apr 25, 2012 at 6:14 AM, Patrick Hausmann wrote: > Hello, > > I am trying to get a new vector 'x1' based on the not NA-values in column > 'a' and 'b'. I found a way but I am sure this is not the best solution. So > any ideas on how to "optimize" this would be great! If by optimize you mea

[R] Create new Vector based on two colums

2012-04-25 Thread Patrick Hausmann
Hello, I am trying to get a new vector 'x1' based on the not NA-values in column 'a' and 'b'. I found a way but I am sure this is not the best solution. So any ideas on how to "optimize" this would be great! m <- factor(c("a1", "a1", "a2", "b1", "b2", "b3", "d1", "d1"), ordered = TRUE) df

Re: [R] Create new string of same length as entry in dataframe

2010-11-24 Thread danobolg321
Simple, but was taking me a long time to google. Thanks Phil, happy Thanksgiving. -- View this message in context: http://r.789695.n4.nabble.com/Create-new-string-of-same-length-as-entry-in-dataframe-tp3058232p3058275.html Sent from the R help mailing list archive at Nabble.com.

Re: [R] Create new string of same length as entry in dataframe

2010-11-24 Thread Phil Spector
Suppose you want to replace using all b's: thestring = 'bb' t2$V3 = substring(thestring,1,t2$V2) t2$V3 [1] "bbb""" "bb" "b" "bb" For the second case, thestring = paste(rep('bad',5),collapse='') thestring [1] "badbadbadbadbad" t2$V3 = substring(the

[R] Create new string of same length as entry in dataframe

2010-11-24 Thread danobolg321
I suspect that this is simple, but thanks in advance for any advice... I have a dataframe, t2: V1 V2 aaa 3 4 aa6 a1 aa 2 V2 is the length of the string in V1 using nchar(as.character(t1$V1)) I'd like to create a third column, that cont

Re: [R] create new variable: percentile value of variable in data frame

2010-05-30 Thread David Winsemius
On May 30, 2010, at 9:03 AM, Jonathan Beard wrote: Hi Stephan, thanks for your response. It looks like the ecdf() works like it should. I have a quick follow-up: I didn't notice any discussion in the help documents of the methods behind ecdf() and quantile(type=3) being equivalent. It looks

Re: [R] create new variable: percentile value of variable in data frame

2010-05-30 Thread Jonathan Beard
Hi Stephan, thanks for your response. It looks like the ecdf() works like it should. I have a quick follow-up: I didn't notice any discussion in the help documents of the methods behind ecdf() and quantile(type=3) being equivalent. It looks like the results produced by each method are consisten

Re: [R] create new variable: percentile value of variable in data frame

2010-05-28 Thread Stephan Kolassa
Hi Jon, does the empirical cumulative distribution function do what you want? dat$q.score <- ecdf(dat$score)(dat$score) ?ecdf HTH Stephan Jonathan Beard schrieb: Hello all, Thanks in advance for you attention. I would like to generate a third value that represents the quantile value of a va

[R] create new variable: percentile value of variable in data frame

2010-05-28 Thread Jonathan Beard
Hello all, Thanks in advance for you attention. I would like to generate a third value that represents the quantile value of a variable in a data frame. # generating data x <- as.matrix(seq(1:30)) y <- as.matrix(rnorm(30, 20, 7)) tmp1 <- cbind(x,y) dat <- as.data.frame(tmp1) colnames(dat) <- c(

Re: [R] Create new dataframes with dames from dataset...

2008-08-07 Thread Henrique Dallazuanna
You can do this: split(x, list(x$Product, x$Type), drop = TRUE) On Thu, Aug 7, 2008 at 8:21 AM, Lars Modig <[EMAIL PROTECTED]> wrote: > Hi > > I'm wondering if you could save new data frames with in a loop using the > names within the data frame... I.e. If you have a data frame as below > > Produ

[R] Create new dataframes with dames from dataset...

2008-08-07 Thread Lars Modig
Hi I'm wondering if you could save new data frames with in a loop using the names within the data frame... I.e. If you have a data frame as below Product TypeTest A One 67 A Two 55 B One 42 A One 55 I would like to generate new dataframes as

Re: [R] create new column with colnames resulting from paste()

2008-06-26 Thread Daniel Folkinshteyn
no need for a for loop - we can vectorize this: > dt <- data.frame(a = c(1, 2, 3), b = c(3, 2, 2), c = c(1, 3, 5)) > dt a b c 1 1 3 1 2 2 2 3 3 3 2 5 > dt[,paste("test", 1:2, sep="")] = rep(1:2, each=3) > dt a b c test1 test2 1 1 3 1 1 2 2 2 2 3 1 2 3 3 2 5 1 2 on 06

Re: [R] create new column with colnames resulting from paste()

2008-06-26 Thread Dong-hyun Oh
Hi, Fortunately, I found a way. -- > dt <- data.frame(a = c(1, 2, 3), b = c(3, 2, 2), c = c(1, 3, 5)) > > for(i in 1:2){ >dt[, paste("test", i, sep = "")] <- rep(i, 3) > } - Thanks. Best, Dong-hyun Oh On Jun 26, 2008, at 6:34 PM, Jorge Ivan Velez

Re: [R] create new column with colnames resulting from paste()

2008-06-26 Thread Jorge Ivan Velez
Dear Dong-hyun, What about dt <- data.frame(a = c(1, 2, 3), b = c(3, 2, 2), c = c(1, 3, 5)) test=matrix(rep(c(1,2),each=3),ncol=2) colnames(test)=paste('test',1:2,sep="") cbind(dt,test) ? HTH, Jorge On Thu, Jun 26, 2008 at 12:23 PM, Dong-hyun Oh <[EMAIL PROTECTED]> wrote: > Dear UseRs, > > I

Re: [R] create new column with colnames resulting from paste()

2008-06-26 Thread jim holtman
Is this what you want > dt <- data.frame(a = c(1, 2, 3), b = c(3, 2, 2), c = c(1, 3, 5)) > for (i in 1:2){ + dt[[paste('test',i,sep="")]] <- rep(i,3) + } > dt a b c test1 test2 1 1 3 1 1 2 2 2 2 3 1 2 3 3 2 5 1 2 > On Thu, Jun 26, 2008 at 12:23 PM, Dong-hyun Oh <[EM

[R] create new column with colnames resulting from paste()

2008-06-26 Thread Dong-hyun Oh
Dear UseRs, I would like to know the way to create a new column by naming it simultaneously. For example, in for() loop I want to create columns named as paste("test", i, sep = ""), as shown below. dt <- data.frame(a = c(1, 2, 3), b = c(3, 2, 2), c = c(1, 3, 5))