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,
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:
>
>
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
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
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
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
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.
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
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
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
?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
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
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
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
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
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,]
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
-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
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
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
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",&
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
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
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")),
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
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
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
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)] <-
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
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
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
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
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
#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
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
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:
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
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
-
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
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
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
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
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
> -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
#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
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(
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
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
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]]
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.
#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
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
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.
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
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
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
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
,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
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
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
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
[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
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=
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
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
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
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.
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
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
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
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
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
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(
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
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
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
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
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
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
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))
80 matches
Mail list logo