create

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

create

Val-17
Hi All,
I have a data frame  with several  columns  and I want to  create
another  column  by using  the values of the other columns.  My
problem is that some the row values  for some columns  have missing
values  and I could not get  the result I waned .

Here is the sample of my data and my attempt.

vdat<-read.table(text="obs, Year, x1, x2, x3
1,  2001, 25 ,10, 10
2,  2001,  ,  15, 25
3,  2001,  50, 10,
4,  2001,  20, , 60",sep=",",header=TRUE,stringsAsFactors=F)
vdat$xy <- 0
vdat$xy <- 2*(vdat$x1) + 5*(vdat$x2) + 3*(vdat$x3)
vdat

     obs Year x1 x2 x3  xy
1   1 2001 25 10 10 130
2   2 2001 NA 15 25  NA
3   3 2001 50 10 NA  NA
4   4 2001 20 NA 60  NA

The desired result si this,

   obs Year x1 x2 x3   xy
1   1 2001 25 10 10   130
2   2 2001 NA 15 25  150
3   3 2001 50 10 NA  150
4   4 2001 20 NA 60  220

How do I get my desired result?
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: create

Jim Lemon-4
Hi Val,
For this particular problem, you can just replace NAs with zeros.

vdat[is.na(vdat)]<-0
vdat$xy <- 2*(vdat$x1) + 5*(vdat$x2) + 3*(vdat$x3)
vdat
 obs Year x1 x2 x3  xy
1   1 2001 25 10 10 130
2   2 2001  0 15 25 150
3   3 2001 50 10  0 150
4   4 2001 20  0 60 220

Note that this is not a general solution to the problem of NA values.

Jim

On Sun, Apr 14, 2019 at 12:54 PM Val <[hidden email]> wrote:

>
> Hi All,
> I have a data frame  with several  columns  and I want to  create
> another  column  by using  the values of the other columns.  My
> problem is that some the row values  for some columns  have missing
> values  and I could not get  the result I waned .
>
> Here is the sample of my data and my attempt.
>
> vdat<-read.table(text="obs, Year, x1, x2, x3
> 1,  2001, 25 ,10, 10
> 2,  2001,  ,  15, 25
> 3,  2001,  50, 10,
> 4,  2001,  20, , 60",sep=",",header=TRUE,stringsAsFactors=F)
> vdat$xy <- 0
> vdat$xy <- 2*(vdat$x1) + 5*(vdat$x2) + 3*(vdat$x3)
> vdat
>
>      obs Year x1 x2 x3  xy
> 1   1 2001 25 10 10 130
> 2   2 2001 NA 15 25  NA
> 3   3 2001 50 10 NA  NA
> 4   4 2001 20 NA 60  NA
>
> The desired result si this,
>
>    obs Year x1 x2 x3   xy
> 1   1 2001 25 10 10   130
> 2   2 2001 NA 15 25  150
> 3   3 2001 50 10 NA  150
> 4   4 2001 20 NA 60  220
>
> How do I get my desired result?
> 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: create

Bert Gunter-2
In reply to this post by Val-17
If the NA's are really 0's, replace them with 0 before doing the
calculation. (see ?is.na).
If they are not 0's, think again about doing this as the results would
probably mislead.

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, Apr 13, 2019 at 7:54 PM Val <[hidden email]> wrote:

> Hi All,
> I have a data frame  with several  columns  and I want to  create
> another  column  by using  the values of the other columns.  My
> problem is that some the row values  for some columns  have missing
> values  and I could not get  the result I waned .
>
> Here is the sample of my data and my attempt.
>
> vdat<-read.table(text="obs, Year, x1, x2, x3
> 1,  2001, 25 ,10, 10
> 2,  2001,  ,  15, 25
> 3,  2001,  50, 10,
> 4,  2001,  20, , 60",sep=",",header=TRUE,stringsAsFactors=F)
> vdat$xy <- 0
> vdat$xy <- 2*(vdat$x1) + 5*(vdat$x2) + 3*(vdat$x3)
> vdat
>
>      obs Year x1 x2 x3  xy
> 1   1 2001 25 10 10 130
> 2   2 2001 NA 15 25  NA
> 3   3 2001 50 10 NA  NA
> 4   4 2001 20 NA 60  NA
>
> The desired result si this,
>
>    obs Year x1 x2 x3   xy
> 1   1 2001 25 10 10   130
> 2   2 2001 NA 15 25  150
> 3   3 2001 50 10 NA  150
> 4   4 2001 20 NA 60  220
>
> How do I get my desired result?
> 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: create

Val-17
In reply to this post by Jim Lemon-4
Hi Bert and Jim,
Thank you for the suggestion.
However, those missing values should not be replaced by 0's.
I want exclude those missing values from the calculation and create
the index using only the non-missing values.


On Sat, Apr 13, 2019 at 10:14 PM Jim Lemon <[hidden email]> wrote:

>
> Hi Val,
> For this particular problem, you can just replace NAs with zeros.
>
> vdat[is.na(vdat)]<-0
> vdat$xy <- 2*(vdat$x1) + 5*(vdat$x2) + 3*(vdat$x3)
> vdat
>  obs Year x1 x2 x3  xy
> 1   1 2001 25 10 10 130
> 2   2 2001  0 15 25 150
> 3   3 2001 50 10  0 150
> 4   4 2001 20  0 60 220
>
> Note that this is not a general solution to the problem of NA values.
>
> Jim
>
> On Sun, Apr 14, 2019 at 12:54 PM Val <[hidden email]> wrote:
> >
> > Hi All,
> > I have a data frame  with several  columns  and I want to  create
> > another  column  by using  the values of the other columns.  My
> > problem is that some the row values  for some columns  have missing
> > values  and I could not get  the result I waned .
> >
> > Here is the sample of my data and my attempt.
> >
> > vdat<-read.table(text="obs, Year, x1, x2, x3
> > 1,  2001, 25 ,10, 10
> > 2,  2001,  ,  15, 25
> > 3,  2001,  50, 10,
> > 4,  2001,  20, , 60",sep=",",header=TRUE,stringsAsFactors=F)
> > vdat$xy <- 0
> > vdat$xy <- 2*(vdat$x1) + 5*(vdat$x2) + 3*(vdat$x3)
> > vdat
> >
> >      obs Year x1 x2 x3  xy
> > 1   1 2001 25 10 10 130
> > 2   2 2001 NA 15 25  NA
> > 3   3 2001 50 10 NA  NA
> > 4   4 2001 20 NA 60  NA
> >
> > The desired result si this,
> >
> >    obs Year x1 x2 x3   xy
> > 1   1 2001 25 10 10   130
> > 2   2 2001 NA 15 25  150
> > 3   3 2001 50 10 NA  150
> > 4   4 2001 20 NA 60  220
> >
> > How do I get my desired result?
> > 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: create

Jeff Newmiller
Looks to me like your initial request contradicts your clarification. Can you explain this discrepancy?

On April 13, 2019 8:29:59 PM PDT, Val <[hidden email]> wrote:

>Hi Bert and Jim,
>Thank you for the suggestion.
>However, those missing values should not be replaced by 0's.
>I want exclude those missing values from the calculation and create
>the index using only the non-missing values.
>
>
>On Sat, Apr 13, 2019 at 10:14 PM Jim Lemon <[hidden email]>
>wrote:
>>
>> Hi Val,
>> For this particular problem, you can just replace NAs with zeros.
>>
>> vdat[is.na(vdat)]<-0
>> vdat$xy <- 2*(vdat$x1) + 5*(vdat$x2) + 3*(vdat$x3)
>> vdat
>>  obs Year x1 x2 x3  xy
>> 1   1 2001 25 10 10 130
>> 2   2 2001  0 15 25 150
>> 3   3 2001 50 10  0 150
>> 4   4 2001 20  0 60 220
>>
>> Note that this is not a general solution to the problem of NA values.
>>
>> Jim
>>
>> On Sun, Apr 14, 2019 at 12:54 PM Val <[hidden email]> wrote:
>> >
>> > Hi All,
>> > I have a data frame  with several  columns  and I want to  create
>> > another  column  by using  the values of the other columns.  My
>> > problem is that some the row values  for some columns  have missing
>> > values  and I could not get  the result I waned .
>> >
>> > Here is the sample of my data and my attempt.
>> >
>> > vdat<-read.table(text="obs, Year, x1, x2, x3
>> > 1,  2001, 25 ,10, 10
>> > 2,  2001,  ,  15, 25
>> > 3,  2001,  50, 10,
>> > 4,  2001,  20, , 60",sep=",",header=TRUE,stringsAsFactors=F)
>> > vdat$xy <- 0
>> > vdat$xy <- 2*(vdat$x1) + 5*(vdat$x2) + 3*(vdat$x3)
>> > vdat
>> >
>> >      obs Year x1 x2 x3  xy
>> > 1   1 2001 25 10 10 130
>> > 2   2 2001 NA 15 25  NA
>> > 3   3 2001 50 10 NA  NA
>> > 4   4 2001 20 NA 60  NA
>> >
>> > The desired result si this,
>> >
>> >    obs Year x1 x2 x3   xy
>> > 1   1 2001 25 10 10   130
>> > 2   2 2001 NA 15 25  150
>> > 3   3 2001 50 10 NA  150
>> > 4   4 2001 20 NA 60  220
>> >
>> > How do I get my desired result?
>> > 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.

--
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: create

Val-17
Sorry for the confusion, my sample data  does not represent  the
actual data set.

The range of  value can be  from -ve to +ve values and 0 could be a
true value of an observation. So, instead of replacing  missing value
by zero, I want  exclude them  from the  calculation.

On Sat, Apr 13, 2019 at 10:42 PM Jeff Newmiller
<[hidden email]> wrote:

>
> Looks to me like your initial request contradicts your clarification. Can you explain this discrepancy?
>
> On April 13, 2019 8:29:59 PM PDT, Val <[hidden email]> wrote:
> >Hi Bert and Jim,
> >Thank you for the suggestion.
> >However, those missing values should not be replaced by 0's.
> >I want exclude those missing values from the calculation and create
> >the index using only the non-missing values.
> >
> >
> >On Sat, Apr 13, 2019 at 10:14 PM Jim Lemon <[hidden email]>
> >wrote:
> >>
> >> Hi Val,
> >> For this particular problem, you can just replace NAs with zeros.
> >>
> >> vdat[is.na(vdat)]<-0
> >> vdat$xy <- 2*(vdat$x1) + 5*(vdat$x2) + 3*(vdat$x3)
> >> vdat
> >>  obs Year x1 x2 x3  xy
> >> 1   1 2001 25 10 10 130
> >> 2   2 2001  0 15 25 150
> >> 3   3 2001 50 10  0 150
> >> 4   4 2001 20  0 60 220
> >>
> >> Note that this is not a general solution to the problem of NA values.
> >>
> >> Jim
> >>
> >> On Sun, Apr 14, 2019 at 12:54 PM Val <[hidden email]> wrote:
> >> >
> >> > Hi All,
> >> > I have a data frame  with several  columns  and I want to  create
> >> > another  column  by using  the values of the other columns.  My
> >> > problem is that some the row values  for some columns  have missing
> >> > values  and I could not get  the result I waned .
> >> >
> >> > Here is the sample of my data and my attempt.
> >> >
> >> > vdat<-read.table(text="obs, Year, x1, x2, x3
> >> > 1,  2001, 25 ,10, 10
> >> > 2,  2001,  ,  15, 25
> >> > 3,  2001,  50, 10,
> >> > 4,  2001,  20, , 60",sep=",",header=TRUE,stringsAsFactors=F)
> >> > vdat$xy <- 0
> >> > vdat$xy <- 2*(vdat$x1) + 5*(vdat$x2) + 3*(vdat$x3)
> >> > vdat
> >> >
> >> >      obs Year x1 x2 x3  xy
> >> > 1   1 2001 25 10 10 130
> >> > 2   2 2001 NA 15 25  NA
> >> > 3   3 2001 50 10 NA  NA
> >> > 4   4 2001 20 NA 60  NA
> >> >
> >> > The desired result si this,
> >> >
> >> >    obs Year x1 x2 x3   xy
> >> > 1   1 2001 25 10 10   130
> >> > 2   2 2001 NA 15 25  150
> >> > 3   3 2001 50 10 NA  150
> >> > 4   4 2001 20 NA 60  220
> >> >
> >> > How do I get my desired result?
> >> > 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.
>
> --
> 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: [EXTERNAL] Re: create

R help mailing list-2
how about one of the following?

vdat$xy <- 2 * (ifelse(is.na(vdat$x1), 0, vdat$x1)) + 5 *
(ifelse(is.na(vdat$x2),
0, vdat$x2)) + 3 * (ifelse(is.na(vdat$x3), 0, vdat$x3))

vdat$xy <- ifelse(is.na(as.matrix(vdat[, paste0("x", 1:3)])), 0,
as.matrix(vdat[, paste0("x", 1:3)])) %*% c(2, 5, 3)

On Sat, Apr 13, 2019 at 9:02 PM Val <[hidden email]> wrote:

> Sorry for the confusion, my sample data  does not represent  the
> actual data set.
>
> The range of  value can be  from -ve to +ve values and 0 could be a
> true value of an observation. So, instead of replacing  missing value
> by zero, I want  exclude them  from the  calculation.
>
> On Sat, Apr 13, 2019 at 10:42 PM Jeff Newmiller
> <[hidden email]> wrote:
> >
> > Looks to me like your initial request contradicts your clarification.
> Can you explain this discrepancy?
> >
> > On April 13, 2019 8:29:59 PM PDT, Val <[hidden email]> wrote:
> > >Hi Bert and Jim,
> > >Thank you for the suggestion.
> > >However, those missing values should not be replaced by 0's.
> > >I want exclude those missing values from the calculation and create
> > >the index using only the non-missing values.
> > >
> > >
> > >On Sat, Apr 13, 2019 at 10:14 PM Jim Lemon <[hidden email]>
> > >wrote:
> > >>
> > >> Hi Val,
> > >> For this particular problem, you can just replace NAs with zeros.
> > >>
> > >> vdat[is.na(vdat)]<-0
> > >> vdat$xy <- 2*(vdat$x1) + 5*(vdat$x2) + 3*(vdat$x3)
> > >> vdat
> > >>  obs Year x1 x2 x3  xy
> > >> 1   1 2001 25 10 10 130
> > >> 2   2 2001  0 15 25 150
> > >> 3   3 2001 50 10  0 150
> > >> 4   4 2001 20  0 60 220
> > >>
> > >> Note that this is not a general solution to the problem of NA values.
> > >>
> > >> Jim
> > >>
> > >> On Sun, Apr 14, 2019 at 12:54 PM Val <[hidden email]> wrote:
> > >> >
> > >> > Hi All,
> > >> > I have a data frame  with several  columns  and I want to  create
> > >> > another  column  by using  the values of the other columns.  My
> > >> > problem is that some the row values  for some columns  have missing
> > >> > values  and I could not get  the result I waned .
> > >> >
> > >> > Here is the sample of my data and my attempt.
> > >> >
> > >> > vdat<-read.table(text="obs, Year, x1, x2, x3
> > >> > 1,  2001, 25 ,10, 10
> > >> > 2,  2001,  ,  15, 25
> > >> > 3,  2001,  50, 10,
> > >> > 4,  2001,  20, , 60",sep=",",header=TRUE,stringsAsFactors=F)
> > >> > vdat$xy <- 0
> > >> > vdat$xy <- 2*(vdat$x1) + 5*(vdat$x2) + 3*(vdat$x3)
> > >> > vdat
> > >> >
> > >> >      obs Year x1 x2 x3  xy
> > >> > 1   1 2001 25 10 10 130
> > >> > 2   2 2001 NA 15 25  NA
> > >> > 3   3 2001 50 10 NA  NA
> > >> > 4   4 2001 20 NA 60  NA
> > >> >
> > >> > The desired result si this,
> > >> >
> > >> >    obs Year x1 x2 x3   xy
> > >> > 1   1 2001 25 10 10   130
> > >> > 2   2 2001 NA 15 25  150
> > >> > 3   3 2001 50 10 NA  150
> > >> > 4   4 2001 20 NA 60  220
> > >> >
> > >> > How do I get my desired result?
> > >> > 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.
> >
> > --
> > 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.
>


--
Dan Dalthorp, PhD
USGS Forest and Rangeland Ecosystem Science Center
Forest Sciences Lab, Rm 311
3200 SW Jefferson Way
Corvallis, OR 97331
ph: 541-750-0953
[hidden email]

        [[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: create

Bert Gunter-2
In reply to this post by Val-17
Just use a temporary variable!

## You should apply is.na() only to the numeric columns you need, not the
whole data frame
## see ?'[' for why.
vnew <- vdat[,3:5]
vnew[is.na(vnew)] <- 0
vdat$xy <- as.matrix(vnew) %*% c(2, 5, 3)
vdat

I still question whether this is wise; but that's for you to determine.


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, Apr 13, 2019 at 9:01 PM Val <[hidden email]> wrote:

> Sorry for the confusion, my sample data  does not represent  the
> actual data set.
>
> The range of  value can be  from -ve to +ve values and 0 could be a
> true value of an observation. So, instead of replacing  missing value
> by zero, I want  exclude them  from the  calculation.
>
> On Sat, Apr 13, 2019 at 10:42 PM Jeff Newmiller
> <[hidden email]> wrote:
> >
> > Looks to me like your initial request contradicts your clarification.
> Can you explain this discrepancy?
> >
> > On April 13, 2019 8:29:59 PM PDT, Val <[hidden email]> wrote:
> > >Hi Bert and Jim,
> > >Thank you for the suggestion.
> > >However, those missing values should not be replaced by 0's.
> > >I want exclude those missing values from the calculation and create
> > >the index using only the non-missing values.
> > >
> > >
> > >On Sat, Apr 13, 2019 at 10:14 PM Jim Lemon <[hidden email]>
> > >wrote:
> > >>
> > >> Hi Val,
> > >> For this particular problem, you can just replace NAs with zeros.
> > >>
> > >> vdat[is.na(vdat)]<-0
> > >> vdat$xy <- 2*(vdat$x1) + 5*(vdat$x2) + 3*(vdat$x3)
> > >> vdat
> > >>  obs Year x1 x2 x3  xy
> > >> 1   1 2001 25 10 10 130
> > >> 2   2 2001  0 15 25 150
> > >> 3   3 2001 50 10  0 150
> > >> 4   4 2001 20  0 60 220
> > >>
> > >> Note that this is not a general solution to the problem of NA values.
> > >>
> > >> Jim
> > >>
> > >> On Sun, Apr 14, 2019 at 12:54 PM Val <[hidden email]> wrote:
> > >> >
> > >> > Hi All,
> > >> > I have a data frame  with several  columns  and I want to  create
> > >> > another  column  by using  the values of the other columns.  My
> > >> > problem is that some the row values  for some columns  have missing
> > >> > values  and I could not get  the result I waned .
> > >> >
> > >> > Here is the sample of my data and my attempt.
> > >> >
> > >> > vdat<-read.table(text="obs, Year, x1, x2, x3
> > >> > 1,  2001, 25 ,10, 10
> > >> > 2,  2001,  ,  15, 25
> > >> > 3,  2001,  50, 10,
> > >> > 4,  2001,  20, , 60",sep=",",header=TRUE,stringsAsFactors=F)
> > >> > vdat$xy <- 0
> > >> > vdat$xy <- 2*(vdat$x1) + 5*(vdat$x2) + 3*(vdat$x3)
> > >> > vdat
> > >> >
> > >> >      obs Year x1 x2 x3  xy
> > >> > 1   1 2001 25 10 10 130
> > >> > 2   2 2001 NA 15 25  NA
> > >> > 3   3 2001 50 10 NA  NA
> > >> > 4   4 2001 20 NA 60  NA
> > >> >
> > >> > The desired result si this,
> > >> >
> > >> >    obs Year x1 x2 x3   xy
> > >> > 1   1 2001 25 10 10   130
> > >> > 2   2 2001 NA 15 25  150
> > >> > 3   3 2001 50 10 NA  150
> > >> > 4   4 2001 20 NA 60  220
> > >> >
> > >> > How do I get my desired result?
> > >> > 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.
> >
> > --
> > 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.
>

        [[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.