new_index

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

new_index

Val-17
Hi All,

I have two data frames   with thousand  rows  and several columns. My
samples of the data frames are shown below

dat1 <-read.table(text="ID, x, y, z
ID , x, y, z
A, 10,  34, 12
B, 25,  42, 18
C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)

dat2 <-read.table(text="ID, x, y, z
ID, weight
A,  0.25
B,  0.42
C,  0.65 ",sep=",",header=TRUE,stringsAsFactors=F)

My goal is to  create an index value  for each ID  by mutliplying the
first row of dat1 by the second  column of dat2.

  (10*0.25 ) + (34*0.42) + (12*0.65)=  24.58
  (25*0.25 ) + (42*0.42) + (18*0.65)=  35.59
  (14*0.25 ) + (20*0.42) + (  8*0.65)=  19.03

The  desired out put is
dat3
ID, Index
A 24.58
B  35.59
C  19.03

How do I do it in an efficent way?

Thank you,

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: new_index

Val-17
Hi  all

Correction for my previous posting.
dat2 should be read as
dat2 <-read.table(text="ID, weight
A,  0.25
B,  0.42
C,  0.65 ",sep=",",header=TRUE,stringsAsFactors=F)

On Sat, Sep 7, 2019 at 1:46 PM Val <[hidden email]> wrote:

>
> Hi All,
>
> I have two data frames   with thousand  rows  and several columns. My
> samples of the data frames are shown below
>
> dat1 <-read.table(text="ID, x, y, z
> ID , x, y, z
> A, 10,  34, 12
> B, 25,  42, 18
> C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)
>
> dat2 <-read.table(text="ID, x, y, z
> ID, weight
> A,  0.25
> B,  0.42
> C,  0.65 ",sep=",",header=TRUE,stringsAsFactors=F)
>
> My goal is to  create an index value  for each ID  by mutliplying the
> first row of dat1 by the second  column of dat2.
>
>   (10*0.25 ) + (34*0.42) + (12*0.65)=  24.58
>   (25*0.25 ) + (42*0.42) + (18*0.65)=  35.59
>   (14*0.25 ) + (20*0.42) + (  8*0.65)=  19.03
>
> The  desired out put is
> dat3
> ID, Index
> A 24.58
> B  35.59
> C  19.03
>
> How do I do it in an efficent way?
>
> Thank you,

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: new_index

Bert Gunter-2
dat1 is wrong also. It should read:

dat1 <-read.table(text="ID, x, y, z
                  A, 10,  34, 12
                  B, 25,  42, 18
                  C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)

Is this a homework problem?  This list has a no homework policy.

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 Sat, Sep 7, 2019 at 12:24 PM Val <[hidden email]> wrote:

> Hi  all
>
> Correction for my previous posting.
> dat2 should be read as
> dat2 <-read.table(text="ID, weight
> A,  0.25
> B,  0.42
> C,  0.65 ",sep=",",header=TRUE,stringsAsFactors=F)
>
> On Sat, Sep 7, 2019 at 1:46 PM Val <[hidden email]> wrote:
> >
> > Hi All,
> >
> > I have two data frames   with thousand  rows  and several columns. My
> > samples of the data frames are shown below
> >
> > dat1 <-read.table(text="ID, x, y, z
> > ID , x, y, z
> > A, 10,  34, 12
> > B, 25,  42, 18
> > C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)
> >
> > dat2 <-read.table(text="ID, x, y, z
> > ID, weight
> > A,  0.25
> > B,  0.42
> > C,  0.65 ",sep=",",header=TRUE,stringsAsFactors=F)
> >
> > My goal is to  create an index value  for each ID  by mutliplying the
> > first row of dat1 by the second  column of dat2.
> >
> >   (10*0.25 ) + (34*0.42) + (12*0.65)=  24.58
> >   (25*0.25 ) + (42*0.42) + (18*0.65)=  35.59
> >   (14*0.25 ) + (20*0.42) + (  8*0.65)=  19.03
> >
> > The  desired out put is
> > dat3
> > ID, Index
> > A 24.58
> > B  35.59
> > C  19.03
> >
> > How do I do it in an efficent way?
> >
> > Thank you,
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: new_index

Rui Barradas
In reply to this post by Val-17
Hello,

The problem itself is simple:

i <- match(dat1$ID, dat2$ID)
colSums(t(dat1[i, -1])*dat2[i, -1])
#    1     2     3
#24.58 35.59 17.10


But both dat1 and dat2 are wrong and can be read with read.csv


dat1 <- read.csv(text = "
ID , x, y, z
A, 10,  34, 12
B, 25,  42, 18
C, 14,  20,  8 ", stringsAsFactors = FALSE)

dat2 <- read.csv(text="
ID, weight
A,  0.25
B,  0.42
C,  0.65 ", stringsAsFactors = FALSE)


Simpler, no?


Hope this helps,

Rui Barradas


Às 20:23 de 07/09/19, Val escreveu:

> Hi  all
>
> Correction for my previous posting.
> dat2 should be read as
> dat2 <-read.table(text="ID, weight
> A,  0.25
> B,  0.42
> C,  0.65 ",sep=",",header=TRUE,stringsAsFactors=F)
>
> On Sat, Sep 7, 2019 at 1:46 PM Val <[hidden email]> wrote:
>>
>> Hi All,
>>
>> I have two data frames   with thousand  rows  and several columns. My
>> samples of the data frames are shown below
>>
>> dat1 <-read.table(text="ID, x, y, z
>> ID , x, y, z
>> A, 10,  34, 12
>> B, 25,  42, 18
>> C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)
>>
>> dat2 <-read.table(text="ID, x, y, z
>> ID, weight
>> A,  0.25
>> B,  0.42
>> C,  0.65 ",sep=",",header=TRUE,stringsAsFactors=F)
>>
>> My goal is to  create an index value  for each ID  by mutliplying the
>> first row of dat1 by the second  column of dat2.
>>
>>    (10*0.25 ) + (34*0.42) + (12*0.65)=  24.58
>>    (25*0.25 ) + (42*0.42) + (18*0.65)=  35.59
>>    (14*0.25 ) + (20*0.42) + (  8*0.65)=  19.03
>>
>> The  desired out put is
>> dat3
>> ID, Index
>> A 24.58
>> B  35.59
>> C  19.03
>>
>> How do I do it in an efficent way?
>>
>> Thank you,
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: new_index

Jeff Newmiller
In reply to this post by Bert Gunter-2
Val has been posting to this list for almost a decade [1] so seems unlikely to be a student... but in all this time has yet to figure out how to post in plain text to avoid corruption of code on this plain text mailing list. The ability to generate small examples has improved, though execution still seems hazy. Why is there an ID column in dat2 at all?

Try

dat3 <- dat1[ 1,, drop=FALSE ]
dat3$Index <- as.matrix( dat1[ -1 ] ) %*% dat2$weight

[1] https://stat.ethz.ch/pipermail/r-help/2010-March/233533.html

On September 7, 2019 12:38:12 PM PDT, Bert Gunter <[hidden email]> wrote:

>dat1 is wrong also. It should read:
>
>dat1 <-read.table(text="ID, x, y, z
>                  A, 10,  34, 12
>                  B, 25,  42, 18
>               C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)
>
>Is this a homework problem?  This list has a no homework policy.
>
>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 Sat, Sep 7, 2019 at 12:24 PM Val <[hidden email]> wrote:
>
>> Hi  all
>>
>> Correction for my previous posting.
>> dat2 should be read as
>> dat2 <-read.table(text="ID, weight
>> A,  0.25
>> B,  0.42
>> C,  0.65 ",sep=",",header=TRUE,stringsAsFactors=F)
>>
>> On Sat, Sep 7, 2019 at 1:46 PM Val <[hidden email]> wrote:
>> >
>> > Hi All,
>> >
>> > I have two data frames   with thousand  rows  and several columns.
>My
>> > samples of the data frames are shown below
>> >
>> > dat1 <-read.table(text="ID, x, y, z
>> > ID , x, y, z
>> > A, 10,  34, 12
>> > B, 25,  42, 18
>> > C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)
>> >
>> > dat2 <-read.table(text="ID, x, y, z
>> > ID, weight
>> > A,  0.25
>> > B,  0.42
>> > C,  0.65 ",sep=",",header=TRUE,stringsAsFactors=F)
>> >
>> > My goal is to  create an index value  for each ID  by mutliplying
>the
>> > first row of dat1 by the second  column of dat2.
>> >
>> >   (10*0.25 ) + (34*0.42) + (12*0.65)=  24.58
>> >   (25*0.25 ) + (42*0.42) + (18*0.65)=  35.59
>> >   (14*0.25 ) + (20*0.42) + (  8*0.65)=  19.03
>> >
>> > The  desired out put is
>> > dat3
>> > ID, Index
>> > A 24.58
>> > B  35.59
>> > C  19.03
>> >
>> > How do I do it in an efficent way?
>> >
>> > Thank you,
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Fwd: new_index

R help mailing list-2
In reply to this post by Val-17
Hi Val, see below:

> dat1 <-read.table(text="ID, x, y, z
+ A, 10,  34, 12
+ B, 25,  42, 18
+ C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)
>
> dat2 <-read.table(text="ID, weight
+ A,  0.25
+ B,  0.42
+ C,  0.65 ",sep=",",header=TRUE,stringsAsFactors=F)
>
> dat3 <- data.frame(ID = dat1[,1], Index = apply(dat1[,-1], 1, FUN= function(x) {sum(x*dat2[,2])} ), stringsAsFactors=F)
> dat3
  ID Index
1  A 24.58
2  B 35.59
3  C 17.10
>
> str(dat3)
'data.frame': 3 obs. of  2 variables:
 $ ID   : chr  "A" "B" "C"
 $ Index: num  24.6 35.6 17.1
>

The first two results "A" and "B" are identical to your example, but
your math in "C" appears a little off.

HTH, Bill.

W. Michels, Ph.D.

On Sat, Sep 7, 2019 at 11:47 AM Val <[hidden email]> wrote:

>
> Hi All,
>
> I have two data frames   with thousand  rows  and several columns. My
> samples of the data frames are shown below
>
> dat1 <-read.table(text="ID, x, y, z
> ID , x, y, z
> A, 10,  34, 12
> B, 25,  42, 18
> C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)
>
> dat2 <-read.table(text="ID, x, y, z
> ID, weight
> A,  0.25
> B,  0.42
> C,  0.65 ",sep=",",header=TRUE,stringsAsFactors=F)
>
> My goal is to  create an index value  for each ID  by mutliplying the
> first row of dat1 by the second  column of dat2.
>
>   (10*0.25 ) + (34*0.42) + (12*0.65)=  24.58
>   (25*0.25 ) + (42*0.42) + (18*0.65)=  35.59
>   (14*0.25 ) + (20*0.42) + (  8*0.65)=  19.03
>
> The  desired out put is
> dat3
> ID, Index
> A 24.58
> B  35.59
> C  19.03
>
> How do I do it in an efficent way?
>
> Thank you,
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: new_index

Val-17
In reply to this post by Jeff Newmiller
Thank you Jeff and all.   I wish to go back to my student life.
ID  is not  necessary in  dat2, sorry for that.

On Sat, Sep 7, 2019 at 5:10 PM Jeff Newmiller <[hidden email]> wrote:

>
> Val has been posting to this list for almost a decade [1] so seems unlikely to be a student... but in all this time has yet to figure out how to post in plain text to avoid corruption of code on this plain text mailing list. The ability to generate small examples has improved, though execution still seems hazy. Why is there an ID column in dat2 at all?
>
> Try
>
> dat3 <- dat1[ 1,, drop=FALSE ]
> dat3$Index <- as.matrix( dat1[ -1 ] ) %*% dat2$weight
>
> [1] https://stat.ethz.ch/pipermail/r-help/2010-March/233533.html
>
> On September 7, 2019 12:38:12 PM PDT, Bert Gunter <[hidden email]> wrote:
> >dat1 is wrong also. It should read:
> >
> >dat1 <-read.table(text="ID, x, y, z
> >                  A, 10,  34, 12
> >                  B, 25,  42, 18
> >               C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)
> >
> >Is this a homework problem?  This list has a no homework policy.
> >
> >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 Sat, Sep 7, 2019 at 12:24 PM Val <[hidden email]> wrote:
> >
> >> Hi  all
> >>
> >> Correction for my previous posting.
> >> dat2 should be read as
> >> dat2 <-read.table(text="ID, weight
> >> A,  0.25
> >> B,  0.42
> >> C,  0.65 ",sep=",",header=TRUE,stringsAsFactors=F)
> >>
> >> On Sat, Sep 7, 2019 at 1:46 PM Val <[hidden email]> wrote:
> >> >
> >> > Hi All,
> >> >
> >> > I have two data frames   with thousand  rows  and several columns.
> >My
> >> > samples of the data frames are shown below
> >> >
> >> > dat1 <-read.table(text="ID, x, y, z
> >> > ID , x, y, z
> >> > A, 10,  34, 12
> >> > B, 25,  42, 18
> >> > C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)
> >> >
> >> > dat2 <-read.table(text="ID, x, y, z
> >> > ID, weight
> >> > A,  0.25
> >> > B,  0.42
> >> > C,  0.65 ",sep=",",header=TRUE,stringsAsFactors=F)
> >> >
> >> > My goal is to  create an index value  for each ID  by mutliplying
> >the
> >> > first row of dat1 by the second  column of dat2.
> >> >
> >> >   (10*0.25 ) + (34*0.42) + (12*0.65)=  24.58
> >> >   (25*0.25 ) + (42*0.42) + (18*0.65)=  35.59
> >> >   (14*0.25 ) + (20*0.42) + (  8*0.65)=  19.03
> >> >
> >> > The  desired out put is
> >> > dat3
> >> > ID, Index
> >> > A 24.58
> >> > B  35.59
> >> > C  19.03
> >> >
> >> > How do I do it in an efficent way?
> >> >
> >> > Thank you,
> >>
> >> ______________________________________________
> >> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide
> >> http://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
> >       [[alternative HTML version deleted]]
> >
> >______________________________________________
> >[hidden email] mailing list -- To UNSUBSCRIBE and more, see
> >https://stat.ethz.ch/mailman/listinfo/r-help
> >PLEASE do read the posting guide
> >http://www.R-project.org/posting-guide.html
> >and provide commented, minimal, self-contained, reproducible code.
>
> --
> Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: new_index

Rui Barradas
In reply to this post by Jeff Newmiller
Hello,

Bert:

Quoting Jeff, "Val has been posting to this list for almost a decade". I
didn't know about the "decade" part (didn't look it up) but that's why I
answered the question.

Val:

It's not that hard to run the code you post *before* posting it. Please.

Rui Barradas

Às 23:10 de 07/09/19, Jeff Newmiller escreveu:

> Val has been posting to this list for almost a decade [1] so seems unlikely to be a student... but in all this time has yet to figure out how to post in plain text to avoid corruption of code on this plain text mailing list. The ability to generate small examples has improved, though execution still seems hazy. Why is there an ID column in dat2 at all?
>
> Try
>
> dat3 <- dat1[ 1,, drop=FALSE ]
> dat3$Index <- as.matrix( dat1[ -1 ] ) %*% dat2$weight
>
> [1] https://stat.ethz.ch/pipermail/r-help/2010-March/233533.html
>
> On September 7, 2019 12:38:12 PM PDT, Bert Gunter <[hidden email]> wrote:
>> dat1 is wrong also. It should read:
>>
>> dat1 <-read.table(text="ID, x, y, z
>>                   A, 10,  34, 12
>>                   B, 25,  42, 18
>>                C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)
>>
>> Is this a homework problem?  This list has a no homework policy.
>>
>> 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 Sat, Sep 7, 2019 at 12:24 PM Val <[hidden email]> wrote:
>>
>>> Hi  all
>>>
>>> Correction for my previous posting.
>>> dat2 should be read as
>>> dat2 <-read.table(text="ID, weight
>>> A,  0.25
>>> B,  0.42
>>> C,  0.65 ",sep=",",header=TRUE,stringsAsFactors=F)
>>>
>>> On Sat, Sep 7, 2019 at 1:46 PM Val <[hidden email]> wrote:
>>>>
>>>> Hi All,
>>>>
>>>> I have two data frames   with thousand  rows  and several columns.
>> My
>>>> samples of the data frames are shown below
>>>>
>>>> dat1 <-read.table(text="ID, x, y, z
>>>> ID , x, y, z
>>>> A, 10,  34, 12
>>>> B, 25,  42, 18
>>>> C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)
>>>>
>>>> dat2 <-read.table(text="ID, x, y, z
>>>> ID, weight
>>>> A,  0.25
>>>> B,  0.42
>>>> C,  0.65 ",sep=",",header=TRUE,stringsAsFactors=F)
>>>>
>>>> My goal is to  create an index value  for each ID  by mutliplying
>> the
>>>> first row of dat1 by the second  column of dat2.
>>>>
>>>>    (10*0.25 ) + (34*0.42) + (12*0.65)=  24.58
>>>>    (25*0.25 ) + (42*0.42) + (18*0.65)=  35.59
>>>>    (14*0.25 ) + (20*0.42) + (  8*0.65)=  19.03
>>>>
>>>> The  desired out put is
>>>> dat3
>>>> ID, Index
>>>> A 24.58
>>>> B  35.59
>>>> C  19.03
>>>>
>>>> How do I do it in an efficent way?
>>>>
>>>> Thank you,
>>>
>>> ______________________________________________
>>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.