include

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

include

Val-17
Hi All,

I am reading a file as follow,

mydat <- read.table(textConnection("Col1 Col2 col3
Z2 NA NA
Z3 X1 NA
Z4 Y1 W1"),header = TRUE)

1. "NA" are   missing  should be replace by 0
2.  value that are in COl2 and Col3  should be included  in col1 before
they appear
in col2 and col3. So the output data looks like as follow,

X1  0  0
Y1  0  0
W1  0  0
Z2  0  0
Z3 X1  0
Z4 Y1 W1

Thank you in advance

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

Jim Lemon-4
Hi Val,
Try this:

preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
 Col2=NA,col3=NA)
rbind(preval,mydat)

Jim

On Sat, Feb 24, 2018 at 3:34 PM, Val <[hidden email]> wrote:

> Hi All,
>
> I am reading a file as follow,
>
> mydat <- read.table(textConnection("Col1 Col2 col3
> Z2 NA NA
> Z3 X1 NA
> Z4 Y1 W1"),header = TRUE)
>
> 1. "NA" are   missing  should be replace by 0
> 2.  value that are in COl2 and Col3  should be included  in col1 before
> they appear
> in col2 and col3. So the output data looks like as follow,
>
> X1  0  0
> Y1  0  0
> W1  0  0
> Z2  0  0
> Z3 X1  0
> Z4 Y1 W1
>
> Thank you in advance
>
>         [[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.
Reply | Threaded
Open this post in threaded view
|

Re: include

Val-17
Thank you Jim

I wanted a final data frame  after replacing the NA's to "0"

x1 =  rbind(unique(preval),mydat)
x2 <- x1[is.na(x1)] <- 0
x2
 but I got this,

[1] 0

why I am getting this?


On Sat, Feb 24, 2018 at 12:17 AM, Jim Lemon <[hidden email]> wrote:

> Hi Val,
> Try this:
>
> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>  Col2=NA,col3=NA)
> rbind(preval,mydat)
>
> Jim
>
> On Sat, Feb 24, 2018 at 3:34 PM, Val <[hidden email]> wrote:
> > Hi All,
> >
> > I am reading a file as follow,
> >
> > mydat <- read.table(textConnection("Col1 Col2 col3
> > Z2 NA NA
> > Z3 X1 NA
> > Z4 Y1 W1"),header = TRUE)
> >
> > 1. "NA" are   missing  should be replace by 0
> > 2.  value that are in COl2 and Col3  should be included  in col1 before
> > they appear
> > in col2 and col3. So the output data looks like as follow,
> >
> > X1  0  0
> > Y1  0  0
> > W1  0  0
> > Z2  0  0
> > Z3 X1  0
> > Z4 Y1 W1
> >
> > Thank you in advance
> >
> >         [[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.
>

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

R help mailing list-2
   x1 =  rbind(unique(preval),mydat)
  x2 <- x1[is.na(x1)] <- 0
  x2  # gives 0

Why introduce the 'x2'?   x1[...] <- 0 alters x1 in place and I think that
altered x1 is what you want.

You asked why x2 was zero.  The value of the expression
   f(a) <- b
and assignments are processed right to left so
   x2 <- x[!is.na(x1)] <- 0
is equivalent to
   x[!is.na(x1)] <- 0
   x2 <- 0


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Sat, Feb 24, 2018 at 9:59 AM, Val <[hidden email]> wrote:

> Thank you Jim
>
> I wanted a final data frame  after replacing the NA's to "0"
>
> x1 =  rbind(unique(preval),mydat)
> x2 <- x1[is.na(x1)] <- 0
> x2
>  but I got this,
>
> [1] 0
>
> why I am getting this?
>
>
> On Sat, Feb 24, 2018 at 12:17 AM, Jim Lemon <[hidden email]> wrote:
>
> > Hi Val,
> > Try this:
> >
> > preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
> >  Col2=NA,col3=NA)
> > rbind(preval,mydat)
> >
> > Jim
> >
> > On Sat, Feb 24, 2018 at 3:34 PM, Val <[hidden email]> wrote:
> > > Hi All,
> > >
> > > I am reading a file as follow,
> > >
> > > mydat <- read.table(textConnection("Col1 Col2 col3
> > > Z2 NA NA
> > > Z3 X1 NA
> > > Z4 Y1 W1"),header = TRUE)
> > >
> > > 1. "NA" are   missing  should be replace by 0
> > > 2.  value that are in COl2 and Col3  should be included  in col1 before
> > > they appear
> > > in col2 and col3. So the output data looks like as follow,
> > >
> > > X1  0  0
> > > Y1  0  0
> > > W1  0  0
> > > Z2  0  0
> > > Z3 X1  0
> > > Z4 Y1 W1
> > >
> > > Thank you in advance
> > >
> > >         [[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.
> >
>
>         [[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.
>

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

Duncan Murdoch-2
On 24/02/2018 1:53 PM, William Dunlap via R-help wrote:

>     x1 =  rbind(unique(preval),mydat)
>    x2 <- x1[is.na(x1)] <- 0
>    x2  # gives 0
>
> Why introduce the 'x2'?   x1[...] <- 0 alters x1 in place and I think that
> altered x1 is what you want.
>
> You asked why x2 was zero.  The value of the expression
>     f(a) <- b
> and assignments are processed right to left so
>     x2 <- x[!is.na(x1)] <- 0
> is equivalent to
>     x[!is.na(x1)] <- 0
>     x2 <- 0

That's not right in general, is it?  I'd think that should be

     x[!is.na(x1)] <- 0
     x2 <- x1

Of course, in this example, x1 is 0, so it gives the same answer.

Duncan Murdoch

>
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
> On Sat, Feb 24, 2018 at 9:59 AM, Val <[hidden email]> wrote:
>
>> Thank you Jim
>>
>> I wanted a final data frame  after replacing the NA's to "0"
>>
>> x1 =  rbind(unique(preval),mydat)
>> x2 <- x1[is.na(x1)] <- 0
>> x2
>>   but I got this,
>>
>> [1] 0
>>
>> why I am getting this?
>>
>>
>> On Sat, Feb 24, 2018 at 12:17 AM, Jim Lemon <[hidden email]> wrote:
>>
>>> Hi Val,
>>> Try this:
>>>
>>> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>>>   Col2=NA,col3=NA)
>>> rbind(preval,mydat)
>>>
>>> Jim
>>>
>>> On Sat, Feb 24, 2018 at 3:34 PM, Val <[hidden email]> wrote:
>>>> Hi All,
>>>>
>>>> I am reading a file as follow,
>>>>
>>>> mydat <- read.table(textConnection("Col1 Col2 col3
>>>> Z2 NA NA
>>>> Z3 X1 NA
>>>> Z4 Y1 W1"),header = TRUE)
>>>>
>>>> 1. "NA" are   missing  should be replace by 0
>>>> 2.  value that are in COl2 and Col3  should be included  in col1 before
>>>> they appear
>>>> in col2 and col3. So the output data looks like as follow,
>>>>
>>>> X1  0  0
>>>> Y1  0  0
>>>> W1  0  0
>>>> Z2  0  0
>>>> Z3 X1  0
>>>> Z4 Y1 W1
>>>>
>>>> Thank you in advance
>>>>
>>>>          [[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.
>>>
>>
>>          [[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.
>>
>
> [[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.
Reply | Threaded
Open this post in threaded view
|

Re: include

Val-17
Thank you Jim   and all, I got it.

I have one more question on the original question

 What does this  "[-1] "  do?
preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
                   Col2=NA,col3=NA)


mydat <- read.table(textConnection("Col1 Col2 col3
Z1 K1 K2
Z2 NA NA
Z3 X1 NA
Z4 Y1 W1"),header = TRUE)

preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
                   Col2=NA,col3=NA)
rbind(unique(preval),mydat)


 Col1 Col2 col3
1 <NA> <NA> <NA>
2   X1 <NA> <NA>
3   Y1 <NA> <NA>
4   K2 <NA> <NA>
5   W1 <NA> <NA>
6   Z1   K1   K2
7   Z2 <NA> <NA>
8   Z3   X1 <NA>
9   Z4   Y1   W1















On Sat, Feb 24, 2018 at 5:04 PM, Duncan Murdoch <[hidden email]>
wrote:

> On 24/02/2018 1:53 PM, William Dunlap via R-help wrote:
>
>>     x1 =  rbind(unique(preval),mydat)
>>    x2 <- x1[is.na(x1)] <- 0
>>    x2  # gives 0
>>
>> Why introduce the 'x2'?   x1[...] <- 0 alters x1 in place and I think that
>> altered x1 is what you want.
>>
>> You asked why x2 was zero.  The value of the expression
>>     f(a) <- b
>> and assignments are processed right to left so
>>     x2 <- x[!is.na(x1)] <- 0
>> is equivalent to
>>     x[!is.na(x1)] <- 0
>>     x2 <- 0
>>
>
> That's not right in general, is it?  I'd think that should be
>
>     x[!is.na(x1)] <- 0
>     x2 <- x1
>
> Of course, in this example, x1 is 0, so it gives the same answer.
>
> Duncan Murdoch
>
>
>
>>
>> Bill Dunlap
>> TIBCO Software
>> wdunlap tibco.com
>>
>> On Sat, Feb 24, 2018 at 9:59 AM, Val <[hidden email]> wrote:
>>
>> Thank you Jim
>>>
>>> I wanted a final data frame  after replacing the NA's to "0"
>>>
>>> x1 =  rbind(unique(preval),mydat)
>>> x2 <- x1[is.na(x1)] <- 0
>>> x2
>>>   but I got this,
>>>
>>> [1] 0
>>>
>>> why I am getting this?
>>>
>>>
>>> On Sat, Feb 24, 2018 at 12:17 AM, Jim Lemon <[hidden email]>
>>> wrote:
>>>
>>> Hi Val,
>>>> Try this:
>>>>
>>>> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>>>>   Col2=NA,col3=NA)
>>>> rbind(preval,mydat)
>>>>
>>>> Jim
>>>>
>>>> On Sat, Feb 24, 2018 at 3:34 PM, Val <[hidden email]> wrote:
>>>>
>>>>> Hi All,
>>>>>
>>>>> I am reading a file as follow,
>>>>>
>>>>> mydat <- read.table(textConnection("Col1 Col2 col3
>>>>> Z2 NA NA
>>>>> Z3 X1 NA
>>>>> Z4 Y1 W1"),header = TRUE)
>>>>>
>>>>> 1. "NA" are   missing  should be replace by 0
>>>>> 2.  value that are in COl2 and Col3  should be included  in col1 before
>>>>> they appear
>>>>> in col2 and col3. So the output data looks like as follow,
>>>>>
>>>>> X1  0  0
>>>>> Y1  0  0
>>>>> W1  0  0
>>>>> Z2  0  0
>>>>> Z3 X1  0
>>>>> Z4 Y1 W1
>>>>>
>>>>> Thank you in advance
>>>>>
>>>>>          [[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.
>>>>>
>>>>
>>>>
>>>          [[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.
>>>
>>>
>>         [[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/posti
>> ng-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: include

Val-17
Sorry , I hit the send key accidentally  here is my complete message.

Thank you Jim   and all, I got it.

I have one more question on the original question

 What does this  "[-1] "  do?
preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
                   Col2=NA,col3=NA)


mydat <- read.table(textConnection("Col1 Col2 col3
Z1 K1 K2
Z2 NA NA
Z3 X1 NA
Z4 Y1 W1"),header = TRUE)

preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
                   Col2=NA,col3=NA)
rbind(unique(preval),mydat)


 Col1 Col2 col3
1 <NA> <NA> <NA>
2   X1 <NA> <NA>
3   Y1 <NA> <NA>
4   K2 <NA> <NA>
5   W1 <NA> <NA>
6   Z1   K1   K2
7   Z2 <NA> <NA>
8   Z3   X1 <NA>
9   Z4   Y1   W1

I could not find K1 in the first   col1. Is that possible to fix this?

On Sat, Feb 24, 2018 at 5:59 PM, Val <[hidden email]> wrote:

> Thank you Jim   and all, I got it.
>
> I have one more question on the original question
>
>  What does this  "[-1] "  do?
> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>                    Col2=NA,col3=NA)
>
>
> mydat <- read.table(textConnection("Col1 Col2 col3
> Z1 K1 K2
> Z2 NA NA
> Z3 X1 NA
> Z4 Y1 W1"),header = TRUE)
>
> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>                    Col2=NA,col3=NA)
> rbind(unique(preval),mydat)
>
>
>  Col1 Col2 col3
> 1 <NA> <NA> <NA>
> 2   X1 <NA> <NA>
> 3   Y1 <NA> <NA>
> 4   K2 <NA> <NA>
> 5   W1 <NA> <NA>
> 6   Z1   K1   K2
> 7   Z2 <NA> <NA>
> 8   Z3   X1 <NA>
> 9   Z4   Y1   W1
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> On Sat, Feb 24, 2018 at 5:04 PM, Duncan Murdoch <[hidden email]>
> wrote:
>
>> On 24/02/2018 1:53 PM, William Dunlap via R-help wrote:
>>
>>>     x1 =  rbind(unique(preval),mydat)
>>>    x2 <- x1[is.na(x1)] <- 0
>>>    x2  # gives 0
>>>
>>> Why introduce the 'x2'?   x1[...] <- 0 alters x1 in place and I think
>>> that
>>> altered x1 is what you want.
>>>
>>> You asked why x2 was zero.  The value of the expression
>>>     f(a) <- b
>>> and assignments are processed right to left so
>>>     x2 <- x[!is.na(x1)] <- 0
>>> is equivalent to
>>>     x[!is.na(x1)] <- 0
>>>     x2 <- 0
>>>
>>
>> That's not right in general, is it?  I'd think that should be
>>
>>     x[!is.na(x1)] <- 0
>>     x2 <- x1
>>
>> Of course, in this example, x1 is 0, so it gives the same answer.
>>
>> Duncan Murdoch
>>
>>
>>
>>>
>>> Bill Dunlap
>>> TIBCO Software
>>> wdunlap tibco.com
>>>
>>> On Sat, Feb 24, 2018 at 9:59 AM, Val <[hidden email]> wrote:
>>>
>>> Thank you Jim
>>>>
>>>> I wanted a final data frame  after replacing the NA's to "0"
>>>>
>>>> x1 =  rbind(unique(preval),mydat)
>>>> x2 <- x1[is.na(x1)] <- 0
>>>> x2
>>>>   but I got this,
>>>>
>>>> [1] 0
>>>>
>>>> why I am getting this?
>>>>
>>>>
>>>> On Sat, Feb 24, 2018 at 12:17 AM, Jim Lemon <[hidden email]>
>>>> wrote:
>>>>
>>>> Hi Val,
>>>>> Try this:
>>>>>
>>>>> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>>>>>   Col2=NA,col3=NA)
>>>>> rbind(preval,mydat)
>>>>>
>>>>> Jim
>>>>>
>>>>> On Sat, Feb 24, 2018 at 3:34 PM, Val <[hidden email]> wrote:
>>>>>
>>>>>> Hi All,
>>>>>>
>>>>>> I am reading a file as follow,
>>>>>>
>>>>>> mydat <- read.table(textConnection("Col1 Col2 col3
>>>>>> Z2 NA NA
>>>>>> Z3 X1 NA
>>>>>> Z4 Y1 W1"),header = TRUE)
>>>>>>
>>>>>> 1. "NA" are   missing  should be replace by 0
>>>>>> 2.  value that are in COl2 and Col3  should be included  in col1
>>>>>> before
>>>>>> they appear
>>>>>> in col2 and col3. So the output data looks like as follow,
>>>>>>
>>>>>> X1  0  0
>>>>>> Y1  0  0
>>>>>> W1  0  0
>>>>>> Z2  0  0
>>>>>> Z3 X1  0
>>>>>> Z4 Y1 W1
>>>>>>
>>>>>> Thank you in advance
>>>>>>
>>>>>>          [[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.
>>>>>>
>>>>>
>>>>>
>>>>          [[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.
>>>>
>>>>
>>>         [[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/posti
>>> ng-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: include

Jim Lemon-4
hi Val,
Your problem seems to be that the data are read in as a factor. The
simplest way I can think of to get around this is:

mydat <- read.table(textConnection("Col1 Col2 col3
Z1 K1 K2
Z2 NA NA
Z3 X1 NA
Z4 Y1 W1"),header = TRUE,stringsAsFactors=FALSE)
preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
 Col2=NA,col3=NA)
rbind(preval,mydat)
mydat[is.na(mydat)]<-"0"

Jiim


On Sun, Feb 25, 2018 at 11:05 AM, Val <[hidden email]> wrote:

> Sorry , I hit the send key accidentally  here is my complete message.
>
> Thank you Jim   and all, I got it.
>
> I have one more question on the original question
>
>  What does this  "[-1] "  do?
> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>                    Col2=NA,col3=NA)
>
>
> mydat <- read.table(textConnection("Col1 Col2 col3
> Z1 K1 K2
> Z2 NA NA
> Z3 X1 NA
> Z4 Y1 W1"),header = TRUE)
>
> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>                    Col2=NA,col3=NA)
> rbind(unique(preval),mydat)
>
>
>  Col1 Col2 col3
> 1 <NA> <NA> <NA>
> 2   X1 <NA> <NA>
> 3   Y1 <NA> <NA>
> 4   K2 <NA> <NA>
> 5   W1 <NA> <NA>
> 6   Z1   K1   K2
> 7   Z2 <NA> <NA>
> 8   Z3   X1 <NA>
> 9   Z4   Y1   W1
>
> I could not find K1 in the first   col1. Is that possible to fix this?
>
> On Sat, Feb 24, 2018 at 5:59 PM, Val <[hidden email]> wrote:
>
>> Thank you Jim   and all, I got it.
>>
>> I have one more question on the original question
>>
>>  What does this  "[-1] "  do?
>> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>>                    Col2=NA,col3=NA)
>>
>>
>> mydat <- read.table(textConnection("Col1 Col2 col3
>> Z1 K1 K2
>> Z2 NA NA
>> Z3 X1 NA
>> Z4 Y1 W1"),header = TRUE)
>>
>> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>>                    Col2=NA,col3=NA)
>> rbind(unique(preval),mydat)
>>
>>
>>  Col1 Col2 col3
>> 1 <NA> <NA> <NA>
>> 2   X1 <NA> <NA>
>> 3   Y1 <NA> <NA>
>> 4   K2 <NA> <NA>
>> 5   W1 <NA> <NA>
>> 6   Z1   K1   K2
>> 7   Z2 <NA> <NA>
>> 8   Z3   X1 <NA>
>> 9   Z4   Y1   W1
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> On Sat, Feb 24, 2018 at 5:04 PM, Duncan Murdoch <[hidden email]>
>> wrote:
>>
>>> On 24/02/2018 1:53 PM, William Dunlap via R-help wrote:
>>>
>>>>     x1 =  rbind(unique(preval),mydat)
>>>>    x2 <- x1[is.na(x1)] <- 0
>>>>    x2  # gives 0
>>>>
>>>> Why introduce the 'x2'?   x1[...] <- 0 alters x1 in place and I think
>>>> that
>>>> altered x1 is what you want.
>>>>
>>>> You asked why x2 was zero.  The value of the expression
>>>>     f(a) <- b
>>>> and assignments are processed right to left so
>>>>     x2 <- x[!is.na(x1)] <- 0
>>>> is equivalent to
>>>>     x[!is.na(x1)] <- 0
>>>>     x2 <- 0
>>>>
>>>
>>> That's not right in general, is it?  I'd think that should be
>>>
>>>     x[!is.na(x1)] <- 0
>>>     x2 <- x1
>>>
>>> Of course, in this example, x1 is 0, so it gives the same answer.
>>>
>>> Duncan Murdoch
>>>
>>>
>>>
>>>>
>>>> Bill Dunlap
>>>> TIBCO Software
>>>> wdunlap tibco.com
>>>>
>>>> On Sat, Feb 24, 2018 at 9:59 AM, Val <[hidden email]> wrote:
>>>>
>>>> Thank you Jim
>>>>>
>>>>> I wanted a final data frame  after replacing the NA's to "0"
>>>>>
>>>>> x1 =  rbind(unique(preval),mydat)
>>>>> x2 <- x1[is.na(x1)] <- 0
>>>>> x2
>>>>>   but I got this,
>>>>>
>>>>> [1] 0
>>>>>
>>>>> why I am getting this?
>>>>>
>>>>>
>>>>> On Sat, Feb 24, 2018 at 12:17 AM, Jim Lemon <[hidden email]>
>>>>> wrote:
>>>>>
>>>>> Hi Val,
>>>>>> Try this:
>>>>>>
>>>>>> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>>>>>>   Col2=NA,col3=NA)
>>>>>> rbind(preval,mydat)
>>>>>>
>>>>>> Jim
>>>>>>
>>>>>> On Sat, Feb 24, 2018 at 3:34 PM, Val <[hidden email]> wrote:
>>>>>>
>>>>>>> Hi All,
>>>>>>>
>>>>>>> I am reading a file as follow,
>>>>>>>
>>>>>>> mydat <- read.table(textConnection("Col1 Col2 col3
>>>>>>> Z2 NA NA
>>>>>>> Z3 X1 NA
>>>>>>> Z4 Y1 W1"),header = TRUE)
>>>>>>>
>>>>>>> 1. "NA" are   missing  should be replace by 0
>>>>>>> 2.  value that are in COl2 and Col3  should be included  in col1
>>>>>>> before
>>>>>>> they appear
>>>>>>> in col2 and col3. So the output data looks like as follow,
>>>>>>>
>>>>>>> X1  0  0
>>>>>>> Y1  0  0
>>>>>>> W1  0  0
>>>>>>> Z2  0  0
>>>>>>> Z3 X1  0
>>>>>>> Z4 Y1 W1
>>>>>>>
>>>>>>> Thank you in advance
>>>>>>>
>>>>>>>          [[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.
>>>>>>>
>>>>>>
>>>>>>
>>>>>          [[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.
>>>>>
>>>>>
>>>>         [[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/posti
>>>> ng-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.
Reply | Threaded
Open this post in threaded view
|

Re: include

Val-17
Thank you Jim,

I read the data as you suggested but I could not find K1 in   col1.

rbind(preval,mydat)  Col1 Col2 col3
1 <NA> <NA> <NA>
2   X1 <NA> <NA>
3   Y1 <NA> <NA>
4   K2 <NA> <NA>
5   W1 <NA> <NA>
6   Z1   K1   K2
7   Z2 <NA> <NA>
8   Z3   X1 <NA>
9   Z4   Y1   W1



On Sat, Feb 24, 2018 at 6:18 PM, Jim Lemon <[hidden email]> wrote:

> hi Val,
> Your problem seems to be that the data are read in as a factor. The
> simplest way I can think of to get around this is:
>
> mydat <- read.table(textConnection("Col1 Col2 col3
> Z1 K1 K2
> Z2 NA NA
> Z3 X1 NA
> Z4 Y1 W1"),header = TRUE,stringsAsFactors=FALSE)
> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>  Col2=NA,col3=NA)
> rbind(preval,mydat)
> mydat[is.na(mydat)]<-"0"
>
> Jiim
>
>
> On Sun, Feb 25, 2018 at 11:05 AM, Val <[hidden email]> wrote:
> > Sorry , I hit the send key accidentally  here is my complete message.
> >
> > Thank you Jim   and all, I got it.
> >
> > I have one more question on the original question
> >
> >  What does this  "[-1] "  do?
> > preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
> >                    Col2=NA,col3=NA)
> >
> >
> > mydat <- read.table(textConnection("Col1 Col2 col3
> > Z1 K1 K2
> > Z2 NA NA
> > Z3 X1 NA
> > Z4 Y1 W1"),header = TRUE)
> >
> > preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
> >                    Col2=NA,col3=NA)
> > rbind(unique(preval),mydat)
> >
> >
> >  Col1 Col2 col3
> > 1 <NA> <NA> <NA>
> > 2   X1 <NA> <NA>
> > 3   Y1 <NA> <NA>
> > 4   K2 <NA> <NA>
> > 5   W1 <NA> <NA>
> > 6   Z1   K1   K2
> > 7   Z2 <NA> <NA>
> > 8   Z3   X1 <NA>
> > 9   Z4   Y1   W1
> >
> > I could not find K1 in the first   col1. Is that possible to fix this?
> >
> > On Sat, Feb 24, 2018 at 5:59 PM, Val <[hidden email]> wrote:
> >
> >> Thank you Jim   and all, I got it.
> >>
> >> I have one more question on the original question
> >>
> >>  What does this  "[-1] "  do?
> >> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
> >>                    Col2=NA,col3=NA)
> >>
> >>
> >> mydat <- read.table(textConnection("Col1 Col2 col3
> >> Z1 K1 K2
> >> Z2 NA NA
> >> Z3 X1 NA
> >> Z4 Y1 W1"),header = TRUE)
> >>
> >> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
> >>                    Col2=NA,col3=NA)
> >> rbind(unique(preval),mydat)
> >>
> >>
> >>  Col1 Col2 col3
> >> 1 <NA> <NA> <NA>
> >> 2   X1 <NA> <NA>
> >> 3   Y1 <NA> <NA>
> >> 4   K2 <NA> <NA>
> >> 5   W1 <NA> <NA>
> >> 6   Z1   K1   K2
> >> 7   Z2 <NA> <NA>
> >> 8   Z3   X1 <NA>
> >> 9   Z4   Y1   W1
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >> On Sat, Feb 24, 2018 at 5:04 PM, Duncan Murdoch <
> [hidden email]>
> >> wrote:
> >>
> >>> On 24/02/2018 1:53 PM, William Dunlap via R-help wrote:
> >>>
> >>>>     x1 =  rbind(unique(preval),mydat)
> >>>>    x2 <- x1[is.na(x1)] <- 0
> >>>>    x2  # gives 0
> >>>>
> >>>> Why introduce the 'x2'?   x1[...] <- 0 alters x1 in place and I think
> >>>> that
> >>>> altered x1 is what you want.
> >>>>
> >>>> You asked why x2 was zero.  The value of the expression
> >>>>     f(a) <- b
> >>>> and assignments are processed right to left so
> >>>>     x2 <- x[!is.na(x1)] <- 0
> >>>> is equivalent to
> >>>>     x[!is.na(x1)] <- 0
> >>>>     x2 <- 0
> >>>>
> >>>
> >>> That's not right in general, is it?  I'd think that should be
> >>>
> >>>     x[!is.na(x1)] <- 0
> >>>     x2 <- x1
> >>>
> >>> Of course, in this example, x1 is 0, so it gives the same answer.
> >>>
> >>> Duncan Murdoch
> >>>
> >>>
> >>>
> >>>>
> >>>> Bill Dunlap
> >>>> TIBCO Software
> >>>> wdunlap tibco.com
> >>>>
> >>>> On Sat, Feb 24, 2018 at 9:59 AM, Val <[hidden email]> wrote:
> >>>>
> >>>> Thank you Jim
> >>>>>
> >>>>> I wanted a final data frame  after replacing the NA's to "0"
> >>>>>
> >>>>> x1 =  rbind(unique(preval),mydat)
> >>>>> x2 <- x1[is.na(x1)] <- 0
> >>>>> x2
> >>>>>   but I got this,
> >>>>>
> >>>>> [1] 0
> >>>>>
> >>>>> why I am getting this?
> >>>>>
> >>>>>
> >>>>> On Sat, Feb 24, 2018 at 12:17 AM, Jim Lemon <[hidden email]>
> >>>>> wrote:
> >>>>>
> >>>>> Hi Val,
> >>>>>> Try this:
> >>>>>>
> >>>>>> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2",
> "col3")]))[-1],
> >>>>>>   Col2=NA,col3=NA)
> >>>>>> rbind(preval,mydat)
> >>>>>>
> >>>>>> Jim
> >>>>>>
> >>>>>> On Sat, Feb 24, 2018 at 3:34 PM, Val <[hidden email]> wrote:
> >>>>>>
> >>>>>>> Hi All,
> >>>>>>>
> >>>>>>> I am reading a file as follow,
> >>>>>>>
> >>>>>>> mydat <- read.table(textConnection("Col1 Col2 col3
> >>>>>>> Z2 NA NA
> >>>>>>> Z3 X1 NA
> >>>>>>> Z4 Y1 W1"),header = TRUE)
> >>>>>>>
> >>>>>>> 1. "NA" are   missing  should be replace by 0
> >>>>>>> 2.  value that are in COl2 and Col3  should be included  in col1
> >>>>>>> before
> >>>>>>> they appear
> >>>>>>> in col2 and col3. So the output data looks like as follow,
> >>>>>>>
> >>>>>>> X1  0  0
> >>>>>>> Y1  0  0
> >>>>>>> W1  0  0
> >>>>>>> Z2  0  0
> >>>>>>> Z3 X1  0
> >>>>>>> Z4 Y1 W1
> >>>>>>>
> >>>>>>> Thank you in advance
> >>>>>>>
> >>>>>>>          [[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.
> >>>>>>>
> >>>>>>
> >>>>>>
> >>>>>          [[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.
> >>>>>
> >>>>>
> >>>>         [[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/posti
> >>>> ng-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.
>

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

Jim Lemon-4
Hi Val,
My fault - I assumed that the NA would be first in the result produced
by "unique":

mydat <- read.table(textConnection("Col1 Col2 col3
Z1 K1 K2
Z2 NA NA
Z3 X1 NA
Z4 Y1 W1"),header = TRUE,stringsAsFactors=FALSE)
val23<-unique(unlist(mydat[,c("Col2","col3")]))
napos<-which(is.na(val23))
preval<-data.frame(Col1=val23[-napos],
 Col2=NA,col3=NA)
mydat<-rbind(preval,mydat)
mydat[is.na(mydat)]<-"0"
mydat

Jim

On Sun, Feb 25, 2018 at 11:27 AM, Val <[hidden email]> wrote:

> Thank you Jim,
>
> I read the data as you suggested but I could not find K1 in   col1.
>
> rbind(preval,mydat)
>   Col1 Col2 col3
> 1 <NA> <NA> <NA>
> 2   X1 <NA> <NA>
> 3   Y1 <NA> <NA>
> 4   K2 <NA> <NA>
> 5   W1 <NA> <NA>
> 6   Z1   K1   K2
> 7   Z2 <NA> <NA>
> 8   Z3   X1 <NA>
> 9   Z4   Y1   W1
>
>
>
> On Sat, Feb 24, 2018 at 6:18 PM, Jim Lemon <[hidden email]> wrote:
>>
>> hi Val,
>> Your problem seems to be that the data are read in as a factor. The
>> simplest way I can think of to get around this is:
>>
>> mydat <- read.table(textConnection("Col1 Col2 col3
>> Z1 K1 K2
>> Z2 NA NA
>> Z3 X1 NA
>> Z4 Y1 W1"),header = TRUE,stringsAsFactors=FALSE)
>> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>>  Col2=NA,col3=NA)
>> rbind(preval,mydat)
>> mydat[is.na(mydat)]<-"0"
>>
>> Jiim
>>
>>
>> On Sun, Feb 25, 2018 at 11:05 AM, Val <[hidden email]> wrote:
>> > Sorry , I hit the send key accidentally  here is my complete message.
>> >
>> > Thank you Jim   and all, I got it.
>> >
>> > I have one more question on the original question
>> >
>> >  What does this  "[-1] "  do?
>> > preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>> >                    Col2=NA,col3=NA)
>> >
>> >
>> > mydat <- read.table(textConnection("Col1 Col2 col3
>> > Z1 K1 K2
>> > Z2 NA NA
>> > Z3 X1 NA
>> > Z4 Y1 W1"),header = TRUE)
>> >
>> > preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>> >                    Col2=NA,col3=NA)
>> > rbind(unique(preval),mydat)
>> >
>> >
>> >  Col1 Col2 col3
>> > 1 <NA> <NA> <NA>
>> > 2   X1 <NA> <NA>
>> > 3   Y1 <NA> <NA>
>> > 4   K2 <NA> <NA>
>> > 5   W1 <NA> <NA>
>> > 6   Z1   K1   K2
>> > 7   Z2 <NA> <NA>
>> > 8   Z3   X1 <NA>
>> > 9   Z4   Y1   W1
>> >
>> > I could not find K1 in the first   col1. Is that possible to fix this?
>> >
>> > On Sat, Feb 24, 2018 at 5:59 PM, Val <[hidden email]> wrote:
>> >
>> >> Thank you Jim   and all, I got it.
>> >>
>> >> I have one more question on the original question
>> >>
>> >>  What does this  "[-1] "  do?
>> >> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>> >>                    Col2=NA,col3=NA)
>> >>
>> >>
>> >> mydat <- read.table(textConnection("Col1 Col2 col3
>> >> Z1 K1 K2
>> >> Z2 NA NA
>> >> Z3 X1 NA
>> >> Z4 Y1 W1"),header = TRUE)
>> >>
>> >> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>> >>                    Col2=NA,col3=NA)
>> >> rbind(unique(preval),mydat)
>> >>
>> >>
>> >>  Col1 Col2 col3
>> >> 1 <NA> <NA> <NA>
>> >> 2   X1 <NA> <NA>
>> >> 3   Y1 <NA> <NA>
>> >> 4   K2 <NA> <NA>
>> >> 5   W1 <NA> <NA>
>> >> 6   Z1   K1   K2
>> >> 7   Z2 <NA> <NA>
>> >> 8   Z3   X1 <NA>
>> >> 9   Z4   Y1   W1
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >> On Sat, Feb 24, 2018 at 5:04 PM, Duncan Murdoch
>> >> <[hidden email]>
>> >> wrote:
>> >>
>> >>> On 24/02/2018 1:53 PM, William Dunlap via R-help wrote:
>> >>>
>> >>>>     x1 =  rbind(unique(preval),mydat)
>> >>>>    x2 <- x1[is.na(x1)] <- 0
>> >>>>    x2  # gives 0
>> >>>>
>> >>>> Why introduce the 'x2'?   x1[...] <- 0 alters x1 in place and I think
>> >>>> that
>> >>>> altered x1 is what you want.
>> >>>>
>> >>>> You asked why x2 was zero.  The value of the expression
>> >>>>     f(a) <- b
>> >>>> and assignments are processed right to left so
>> >>>>     x2 <- x[!is.na(x1)] <- 0
>> >>>> is equivalent to
>> >>>>     x[!is.na(x1)] <- 0
>> >>>>     x2 <- 0
>> >>>>
>> >>>
>> >>> That's not right in general, is it?  I'd think that should be
>> >>>
>> >>>     x[!is.na(x1)] <- 0
>> >>>     x2 <- x1
>> >>>
>> >>> Of course, in this example, x1 is 0, so it gives the same answer.
>> >>>
>> >>> Duncan Murdoch
>> >>>
>> >>>
>> >>>
>> >>>>
>> >>>> Bill Dunlap
>> >>>> TIBCO Software
>> >>>> wdunlap tibco.com
>> >>>>
>> >>>> On Sat, Feb 24, 2018 at 9:59 AM, Val <[hidden email]> wrote:
>> >>>>
>> >>>> Thank you Jim
>> >>>>>
>> >>>>> I wanted a final data frame  after replacing the NA's to "0"
>> >>>>>
>> >>>>> x1 =  rbind(unique(preval),mydat)
>> >>>>> x2 <- x1[is.na(x1)] <- 0
>> >>>>> x2
>> >>>>>   but I got this,
>> >>>>>
>> >>>>> [1] 0
>> >>>>>
>> >>>>> why I am getting this?
>> >>>>>
>> >>>>>
>> >>>>> On Sat, Feb 24, 2018 at 12:17 AM, Jim Lemon <[hidden email]>
>> >>>>> wrote:
>> >>>>>
>> >>>>> Hi Val,
>> >>>>>> Try this:
>> >>>>>>
>> >>>>>>
>> >>>>>> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>> >>>>>>   Col2=NA,col3=NA)
>> >>>>>> rbind(preval,mydat)
>> >>>>>>
>> >>>>>> Jim
>> >>>>>>
>> >>>>>> On Sat, Feb 24, 2018 at 3:34 PM, Val <[hidden email]> wrote:
>> >>>>>>
>> >>>>>>> Hi All,
>> >>>>>>>
>> >>>>>>> I am reading a file as follow,
>> >>>>>>>
>> >>>>>>> mydat <- read.table(textConnection("Col1 Col2 col3
>> >>>>>>> Z2 NA NA
>> >>>>>>> Z3 X1 NA
>> >>>>>>> Z4 Y1 W1"),header = TRUE)
>> >>>>>>>
>> >>>>>>> 1. "NA" are   missing  should be replace by 0
>> >>>>>>> 2.  value that are in COl2 and Col3  should be included  in col1
>> >>>>>>> before
>> >>>>>>> they appear
>> >>>>>>> in col2 and col3. So the output data looks like as follow,
>> >>>>>>>
>> >>>>>>> X1  0  0
>> >>>>>>> Y1  0  0
>> >>>>>>> W1  0  0
>> >>>>>>> Z2  0  0
>> >>>>>>> Z3 X1  0
>> >>>>>>> Z4 Y1 W1
>> >>>>>>>
>> >>>>>>> Thank you in advance
>> >>>>>>>
>> >>>>>>>          [[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.
>> >>>>>>>
>> >>>>>>
>> >>>>>>
>> >>>>>          [[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.
>> >>>>>
>> >>>>>
>> >>>>         [[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/posti
>> >>>> ng-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.
Reply | Threaded
Open this post in threaded view
|

Re: include

Val-17
Thank you so much Jim!

On Sat, Feb 24, 2018 at 6:38 PM, Jim Lemon <[hidden email]> wrote:

> Hi Val,
> My fault - I assumed that the NA would be first in the result produced
> by "unique":
>
> mydat <- read.table(textConnection("Col1 Col2 col3
> Z1 K1 K2
> Z2 NA NA
> Z3 X1 NA
> Z4 Y1 W1"),header = TRUE,stringsAsFactors=FALSE)
> val23<-unique(unlist(mydat[,c("Col2","col3")]))
> napos<-which(is.na(val23))
> preval<-data.frame(Col1=val23[-napos],
>  Col2=NA,col3=NA)
> mydat<-rbind(preval,mydat)
> mydat[is.na(mydat)]<-"0"
> mydat
>
> Jim
>
> On Sun, Feb 25, 2018 at 11:27 AM, Val <[hidden email]> wrote:
> > Thank you Jim,
> >
> > I read the data as you suggested but I could not find K1 in   col1.
> >
> > rbind(preval,mydat)
> >   Col1 Col2 col3
> > 1 <NA> <NA> <NA>
> > 2   X1 <NA> <NA>
> > 3   Y1 <NA> <NA>
> > 4   K2 <NA> <NA>
> > 5   W1 <NA> <NA>
> > 6   Z1   K1   K2
> > 7   Z2 <NA> <NA>
> > 8   Z3   X1 <NA>
> > 9   Z4   Y1   W1
> >
> >
> >
> > On Sat, Feb 24, 2018 at 6:18 PM, Jim Lemon <[hidden email]> wrote:
> >>
> >> hi Val,
> >> Your problem seems to be that the data are read in as a factor. The
> >> simplest way I can think of to get around this is:
> >>
> >> mydat <- read.table(textConnection("Col1 Col2 col3
> >> Z1 K1 K2
> >> Z2 NA NA
> >> Z3 X1 NA
> >> Z4 Y1 W1"),header = TRUE,stringsAsFactors=FALSE)
> >> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
> >>  Col2=NA,col3=NA)
> >> rbind(preval,mydat)
> >> mydat[is.na(mydat)]<-"0"
> >>
> >> Jiim
> >>
> >>
> >> On Sun, Feb 25, 2018 at 11:05 AM, Val <[hidden email]> wrote:
> >> > Sorry , I hit the send key accidentally  here is my complete message.
> >> >
> >> > Thank you Jim   and all, I got it.
> >> >
> >> > I have one more question on the original question
> >> >
> >> >  What does this  "[-1] "  do?
> >> > preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
> >> >                    Col2=NA,col3=NA)
> >> >
> >> >
> >> > mydat <- read.table(textConnection("Col1 Col2 col3
> >> > Z1 K1 K2
> >> > Z2 NA NA
> >> > Z3 X1 NA
> >> > Z4 Y1 W1"),header = TRUE)
> >> >
> >> > preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
> >> >                    Col2=NA,col3=NA)
> >> > rbind(unique(preval),mydat)
> >> >
> >> >
> >> >  Col1 Col2 col3
> >> > 1 <NA> <NA> <NA>
> >> > 2   X1 <NA> <NA>
> >> > 3   Y1 <NA> <NA>
> >> > 4   K2 <NA> <NA>
> >> > 5   W1 <NA> <NA>
> >> > 6   Z1   K1   K2
> >> > 7   Z2 <NA> <NA>
> >> > 8   Z3   X1 <NA>
> >> > 9   Z4   Y1   W1
> >> >
> >> > I could not find K1 in the first   col1. Is that possible to fix this?
> >> >
> >> > On Sat, Feb 24, 2018 at 5:59 PM, Val <[hidden email]> wrote:
> >> >
> >> >> Thank you Jim   and all, I got it.
> >> >>
> >> >> I have one more question on the original question
> >> >>
> >> >>  What does this  "[-1] "  do?
> >> >> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2",
> "col3")]))[-1],
> >> >>                    Col2=NA,col3=NA)
> >> >>
> >> >>
> >> >> mydat <- read.table(textConnection("Col1 Col2 col3
> >> >> Z1 K1 K2
> >> >> Z2 NA NA
> >> >> Z3 X1 NA
> >> >> Z4 Y1 W1"),header = TRUE)
> >> >>
> >> >> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2",
> "col3")]))[-1],
> >> >>                    Col2=NA,col3=NA)
> >> >> rbind(unique(preval),mydat)
> >> >>
> >> >>
> >> >>  Col1 Col2 col3
> >> >> 1 <NA> <NA> <NA>
> >> >> 2   X1 <NA> <NA>
> >> >> 3   Y1 <NA> <NA>
> >> >> 4   K2 <NA> <NA>
> >> >> 5   W1 <NA> <NA>
> >> >> 6   Z1   K1   K2
> >> >> 7   Z2 <NA> <NA>
> >> >> 8   Z3   X1 <NA>
> >> >> 9   Z4   Y1   W1
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >> On Sat, Feb 24, 2018 at 5:04 PM, Duncan Murdoch
> >> >> <[hidden email]>
> >> >> wrote:
> >> >>
> >> >>> On 24/02/2018 1:53 PM, William Dunlap via R-help wrote:
> >> >>>
> >> >>>>     x1 =  rbind(unique(preval),mydat)
> >> >>>>    x2 <- x1[is.na(x1)] <- 0
> >> >>>>    x2  # gives 0
> >> >>>>
> >> >>>> Why introduce the 'x2'?   x1[...] <- 0 alters x1 in place and I
> think
> >> >>>> that
> >> >>>> altered x1 is what you want.
> >> >>>>
> >> >>>> You asked why x2 was zero.  The value of the expression
> >> >>>>     f(a) <- b
> >> >>>> and assignments are processed right to left so
> >> >>>>     x2 <- x[!is.na(x1)] <- 0
> >> >>>> is equivalent to
> >> >>>>     x[!is.na(x1)] <- 0
> >> >>>>     x2 <- 0
> >> >>>>
> >> >>>
> >> >>> That's not right in general, is it?  I'd think that should be
> >> >>>
> >> >>>     x[!is.na(x1)] <- 0
> >> >>>     x2 <- x1
> >> >>>
> >> >>> Of course, in this example, x1 is 0, so it gives the same answer.
> >> >>>
> >> >>> Duncan Murdoch
> >> >>>
> >> >>>
> >> >>>
> >> >>>>
> >> >>>> Bill Dunlap
> >> >>>> TIBCO Software
> >> >>>> wdunlap tibco.com
> >> >>>>
> >> >>>> On Sat, Feb 24, 2018 at 9:59 AM, Val <[hidden email]> wrote:
> >> >>>>
> >> >>>> Thank you Jim
> >> >>>>>
> >> >>>>> I wanted a final data frame  after replacing the NA's to "0"
> >> >>>>>
> >> >>>>> x1 =  rbind(unique(preval),mydat)
> >> >>>>> x2 <- x1[is.na(x1)] <- 0
> >> >>>>> x2
> >> >>>>>   but I got this,
> >> >>>>>
> >> >>>>> [1] 0
> >> >>>>>
> >> >>>>> why I am getting this?
> >> >>>>>
> >> >>>>>
> >> >>>>> On Sat, Feb 24, 2018 at 12:17 AM, Jim Lemon <[hidden email]
> >
> >> >>>>> wrote:
> >> >>>>>
> >> >>>>> Hi Val,
> >> >>>>>> Try this:
> >> >>>>>>
> >> >>>>>>
> >> >>>>>> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2",
> "col3")]))[-1],
> >> >>>>>>   Col2=NA,col3=NA)
> >> >>>>>> rbind(preval,mydat)
> >> >>>>>>
> >> >>>>>> Jim
> >> >>>>>>
> >> >>>>>> On Sat, Feb 24, 2018 at 3:34 PM, Val <[hidden email]> wrote:
> >> >>>>>>
> >> >>>>>>> Hi All,
> >> >>>>>>>
> >> >>>>>>> I am reading a file as follow,
> >> >>>>>>>
> >> >>>>>>> mydat <- read.table(textConnection("Col1 Col2 col3
> >> >>>>>>> Z2 NA NA
> >> >>>>>>> Z3 X1 NA
> >> >>>>>>> Z4 Y1 W1"),header = TRUE)
> >> >>>>>>>
> >> >>>>>>> 1. "NA" are   missing  should be replace by 0
> >> >>>>>>> 2.  value that are in COl2 and Col3  should be included  in col1
> >> >>>>>>> before
> >> >>>>>>> they appear
> >> >>>>>>> in col2 and col3. So the output data looks like as follow,
> >> >>>>>>>
> >> >>>>>>> X1  0  0
> >> >>>>>>> Y1  0  0
> >> >>>>>>> W1  0  0
> >> >>>>>>> Z2  0  0
> >> >>>>>>> Z3 X1  0
> >> >>>>>>> Z4 Y1 W1
> >> >>>>>>>
> >> >>>>>>> Thank you in advance
> >> >>>>>>>
> >> >>>>>>>          [[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.
> >> >>>>>>>
> >> >>>>>>
> >> >>>>>>
> >> >>>>>          [[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.
> >> >>>>>
> >> >>>>>
> >> >>>>         [[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/posti
> >> >>>> ng-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.
> >
> >
>

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

R help mailing list-2
In reply to this post by Duncan Murdoch-2
    That's not right in general, is it?  I'd think that should be

       x[!is.na(x1)] <- 0
       x2 <- x1

I left out a '1' on the first line - it should have been
   x1[!is.na(x1)] <- 0

In the following examples the value of the assignment
expression is the right hand side of the assignment.

> x1 <- factor(c("A","B","C","D","E"))
> x2 <- x1[2:5] <- c("C","E")
> str(x1)
 Factor w/ 5 levels "A","B","C","D",..: 1 3 5 3 5
> str(x2)
 chr [1:2] "C" "E"
'

> x1 <- (2:5) + 1i * (5:2)
> x2 <- x1[ Mod(x1) == 5 ] <- c(17L, 19L)
> str(x1)
 cplx [1:4] 2+5i 17+0i 19+0i ...
> str(x2)
 int [1:2] 17 19







Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Sat, Feb 24, 2018 at 3:04 PM, Duncan Murdoch <[hidden email]>
wrote:

> On 24/02/2018 1:53 PM, William Dunlap via R-help wrote:
>
>>     x1 =  rbind(unique(preval),mydat)
>>    x2 <- x1[is.na(x1)] <- 0
>>    x2  # gives 0
>>
>> Why introduce the 'x2'?   x1[...] <- 0 alters x1 in place and I think that
>> altered x1 is what you want.
>>
>> You asked why x2 was zero.  The value of the expression
>>     f(a) <- b
>> and assignments are processed right to left so
>>     x2 <- x[!is.na(x1)] <- 0
>> is equivalent to
>>     x[!is.na(x1)] <- 0
>>     x2 <- 0
>>
>
> That's not right in general, is it?  I'd think that should be
>
>     x[!is.na(x1)] <- 0
>     x2 <- x1
>
> Of course, in this example, x1 is 0, so it gives the same answer.
>
> Duncan Murdoch
>
>
>
>>
>> Bill Dunlap
>> TIBCO Software
>> wdunlap tibco.com
>>
>> On Sat, Feb 24, 2018 at 9:59 AM, Val <[hidden email]> wrote:
>>
>> Thank you Jim
>>>
>>> I wanted a final data frame  after replacing the NA's to "0"
>>>
>>> x1 =  rbind(unique(preval),mydat)
>>> x2 <- x1[is.na(x1)] <- 0
>>> x2
>>>   but I got this,
>>>
>>> [1] 0
>>>
>>> why I am getting this?
>>>
>>>
>>> On Sat, Feb 24, 2018 at 12:17 AM, Jim Lemon <[hidden email]>
>>> wrote:
>>>
>>> Hi Val,
>>>> Try this:
>>>>
>>>> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>>>>   Col2=NA,col3=NA)
>>>> rbind(preval,mydat)
>>>>
>>>> Jim
>>>>
>>>> On Sat, Feb 24, 2018 at 3:34 PM, Val <[hidden email]> wrote:
>>>>
>>>>> Hi All,
>>>>>
>>>>> I am reading a file as follow,
>>>>>
>>>>> mydat <- read.table(textConnection("Col1 Col2 col3
>>>>> Z2 NA NA
>>>>> Z3 X1 NA
>>>>> Z4 Y1 W1"),header = TRUE)
>>>>>
>>>>> 1. "NA" are   missing  should be replace by 0
>>>>> 2.  value that are in COl2 and Col3  should be included  in col1 before
>>>>> they appear
>>>>> in col2 and col3. So the output data looks like as follow,
>>>>>
>>>>> X1  0  0
>>>>> Y1  0  0
>>>>> W1  0  0
>>>>> Z2  0  0
>>>>> Z3 X1  0
>>>>> Z4 Y1 W1
>>>>>
>>>>> Thank you in advance
>>>>>
>>>>>          [[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.
>>>>>
>>>>
>>>>
>>>          [[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.
>>>
>>>
>>         [[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/posti
>> ng-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: include

Val-17
In reply to this post by Jim Lemon-4
HI Jim and all,

I want to put one more condition.   Include col2 and col3 if they are not
in col1.

Here is the data
mydat <- read.table(textConnection("Col1 Col2 col3
K2 X1 NA
Z1 K1 K2
Z2 NA NA
Z3 X1 NA
Z4 Y1 W1"),header = TRUE,stringsAsFactors=FALSE)

The desired out put would be

  Col1 Col2 col3
1    X1    0    0
2    K1    0    0
3    Y1    0    0
4    W1    0    0
6    K2   X1    0
7    Z1   K1   K2
8    Z2    0    0
9    Z3   X1    0
10   Z4   Y1   W1

K2 is already is already in col1 and should not be added.

Thank you in advance







On Sat, Feb 24, 2018 at 6:38 PM, Jim Lemon <[hidden email]> wrote:

> Hi Val,
> My fault - I assumed that the NA would be first in the result produced
> by "unique":
>
> mydat <- read.table(textConnection("Col1 Col2 col3
> Z1 K1 K2
> Z2 NA NA
> Z3 X1 NA
> Z4 Y1 W1"),header = TRUE,stringsAsFactors=FALSE)
> val23<-unique(unlist(mydat[,c("Col2","col3")]))
> napos<-which(is.na(val23))
> preval<-data.frame(Col1=val23[-napos],
>  Col2=NA,col3=NA)
> mydat<-rbind(preval,mydat)
> mydat[is.na(mydat)]<-"0"
> mydat
>
> Jim
>
> On Sun, Feb 25, 2018 at 11:27 AM, Val <[hidden email]> wrote:
> > Thank you Jim,
> >
> > I read the data as you suggested but I could not find K1 in   col1.
> >
> > rbind(preval,mydat)
> >   Col1 Col2 col3
> > 1 <NA> <NA> <NA>
> > 2   X1 <NA> <NA>
> > 3   Y1 <NA> <NA>
> > 4   K2 <NA> <NA>
> > 5   W1 <NA> <NA>
> > 6   Z1   K1   K2
> > 7   Z2 <NA> <NA>
> > 8   Z3   X1 <NA>
> > 9   Z4   Y1   W1
> >
> >
> >
> > On Sat, Feb 24, 2018 at 6:18 PM, Jim Lemon <[hidden email]> wrote:
> >>
> >> hi Val,
> >> Your problem seems to be that the data are read in as a factor. The
> >> simplest way I can think of to get around this is:
> >>
> >> mydat <- read.table(textConnection("Col1 Col2 col3
> >> Z1 K1 K2
> >> Z2 NA NA
> >> Z3 X1 NA
> >> Z4 Y1 W1"),header = TRUE,stringsAsFactors=FALSE)
> >> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
> >>  Col2=NA,col3=NA)
> >> rbind(preval,mydat)
> >> mydat[is.na(mydat)]<-"0"
> >>
> >> Jiim
> >>
> >>
> >> On Sun, Feb 25, 2018 at 11:05 AM, Val <[hidden email]> wrote:
> >> > Sorry , I hit the send key accidentally  here is my complete message.
> >> >
> >> > Thank you Jim   and all, I got it.
> >> >
> >> > I have one more question on the original question
> >> >
> >> >  What does this  "[-1] "  do?
> >> > preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
> >> >                    Col2=NA,col3=NA)
> >> >
> >> >
> >> > mydat <- read.table(textConnection("Col1 Col2 col3
> >> > Z1 K1 K2
> >> > Z2 NA NA
> >> > Z3 X1 NA
> >> > Z4 Y1 W1"),header = TRUE)
> >> >
> >> > preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
> >> >                    Col2=NA,col3=NA)
> >> > rbind(unique(preval),mydat)
> >> >
> >> >
> >> >  Col1 Col2 col3
> >> > 1 <NA> <NA> <NA>
> >> > 2   X1 <NA> <NA>
> >> > 3   Y1 <NA> <NA>
> >> > 4   K2 <NA> <NA>
> >> > 5   W1 <NA> <NA>
> >> > 6   Z1   K1   K2
> >> > 7   Z2 <NA> <NA>
> >> > 8   Z3   X1 <NA>
> >> > 9   Z4   Y1   W1
> >> >
> >> > I could not find K1 in the first   col1. Is that possible to fix this?
> >> >
> >> > On Sat, Feb 24, 2018 at 5:59 PM, Val <[hidden email]> wrote:
> >> >
> >> >> Thank you Jim   and all, I got it.
> >> >>
> >> >> I have one more question on the original question
> >> >>
> >> >>  What does this  "[-1] "  do?
> >> >> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2",
> "col3")]))[-1],
> >> >>                    Col2=NA,col3=NA)
> >> >>
> >> >>
> >> >> mydat <- read.table(textConnection("Col1 Col2 col3
> >> >> Z1 K1 K2
> >> >> Z2 NA NA
> >> >> Z3 X1 NA
> >> >> Z4 Y1 W1"),header = TRUE)
> >> >>
> >> >> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2",
> "col3")]))[-1],
> >> >>                    Col2=NA,col3=NA)
> >> >> rbind(unique(preval),mydat)
> >> >>
> >> >>
> >> >>  Col1 Col2 col3
> >> >> 1 <NA> <NA> <NA>
> >> >> 2   X1 <NA> <NA>
> >> >> 3   Y1 <NA> <NA>
> >> >> 4   K2 <NA> <NA>
> >> >> 5   W1 <NA> <NA>
> >> >> 6   Z1   K1   K2
> >> >> 7   Z2 <NA> <NA>
> >> >> 8   Z3   X1 <NA>
> >> >> 9   Z4   Y1   W1
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >> On Sat, Feb 24, 2018 at 5:04 PM, Duncan Murdoch
> >> >> <[hidden email]>
> >> >> wrote:
> >> >>
> >> >>> On 24/02/2018 1:53 PM, William Dunlap via R-help wrote:
> >> >>>
> >> >>>>     x1 =  rbind(unique(preval),mydat)
> >> >>>>    x2 <- x1[is.na(x1)] <- 0
> >> >>>>    x2  # gives 0
> >> >>>>
> >> >>>> Why introduce the 'x2'?   x1[...] <- 0 alters x1 in place and I
> think
> >> >>>> that
> >> >>>> altered x1 is what you want.
> >> >>>>
> >> >>>> You asked why x2 was zero.  The value of the expression
> >> >>>>     f(a) <- b
> >> >>>> and assignments are processed right to left so
> >> >>>>     x2 <- x[!is.na(x1)] <- 0
> >> >>>> is equivalent to
> >> >>>>     x[!is.na(x1)] <- 0
> >> >>>>     x2 <- 0
> >> >>>>
> >> >>>
> >> >>> That's not right in general, is it?  I'd think that should be
> >> >>>
> >> >>>     x[!is.na(x1)] <- 0
> >> >>>     x2 <- x1
> >> >>>
> >> >>> Of course, in this example, x1 is 0, so it gives the same answer.
> >> >>>
> >> >>> Duncan Murdoch
> >> >>>
> >> >>>
> >> >>>
> >> >>>>
> >> >>>> Bill Dunlap
> >> >>>> TIBCO Software
> >> >>>> wdunlap tibco.com
> >> >>>>
> >> >>>> On Sat, Feb 24, 2018 at 9:59 AM, Val <[hidden email]> wrote:
> >> >>>>
> >> >>>> Thank you Jim
> >> >>>>>
> >> >>>>> I wanted a final data frame  after replacing the NA's to "0"
> >> >>>>>
> >> >>>>> x1 =  rbind(unique(preval),mydat)
> >> >>>>> x2 <- x1[is.na(x1)] <- 0
> >> >>>>> x2
> >> >>>>>   but I got this,
> >> >>>>>
> >> >>>>> [1] 0
> >> >>>>>
> >> >>>>> why I am getting this?
> >> >>>>>
> >> >>>>>
> >> >>>>> On Sat, Feb 24, 2018 at 12:17 AM, Jim Lemon <[hidden email]
> >
> >> >>>>> wrote:
> >> >>>>>
> >> >>>>> Hi Val,
> >> >>>>>> Try this:
> >> >>>>>>
> >> >>>>>>
> >> >>>>>> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2",
> "col3")]))[-1],
> >> >>>>>>   Col2=NA,col3=NA)
> >> >>>>>> rbind(preval,mydat)
> >> >>>>>>
> >> >>>>>> Jim
> >> >>>>>>
> >> >>>>>> On Sat, Feb 24, 2018 at 3:34 PM, Val <[hidden email]> wrote:
> >> >>>>>>
> >> >>>>>>> Hi All,
> >> >>>>>>>
> >> >>>>>>> I am reading a file as follow,
> >> >>>>>>>
> >> >>>>>>> mydat <- read.table(textConnection("Col1 Col2 col3
> >> >>>>>>> Z2 NA NA
> >> >>>>>>> Z3 X1 NA
> >> >>>>>>> Z4 Y1 W1"),header = TRUE)
> >> >>>>>>>
> >> >>>>>>> 1. "NA" are   missing  should be replace by 0
> >> >>>>>>> 2.  value that are in COl2 and Col3  should be included  in col1
> >> >>>>>>> before
> >> >>>>>>> they appear
> >> >>>>>>> in col2 and col3. So the output data looks like as follow,
> >> >>>>>>>
> >> >>>>>>> X1  0  0
> >> >>>>>>> Y1  0  0
> >> >>>>>>> W1  0  0
> >> >>>>>>> Z2  0  0
> >> >>>>>>> Z3 X1  0
> >> >>>>>>> Z4 Y1 W1
> >> >>>>>>>
> >> >>>>>>> Thank you in advance
> >> >>>>>>>
> >> >>>>>>>          [[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.
> >> >>>>>>>
> >> >>>>>>
> >> >>>>>>
> >> >>>>>          [[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.
> >> >>>>>
> >> >>>>>
> >> >>>>         [[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/posti
> >> >>>> ng-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.
> >
> >
>

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

Jeff Newmiller
Jim has been exceedingly patient (and may well continue to be so), but this smells like "failure to launch". At what point will you start showing your (failed) attempts at solving your own problems so we can help you work on your specific weaknesses and become self-sufficient?
--
Sent from my phone. Please excuse my brevity.

On February 25, 2018 7:55:55 AM PST, Val <[hidden email]> wrote:

>HI Jim and all,
>
>I want to put one more condition.   Include col2 and col3 if they are
>not
>in col1.
>
>Here is the data
>mydat <- read.table(textConnection("Col1 Col2 col3
>K2 X1 NA
>Z1 K1 K2
>Z2 NA NA
>Z3 X1 NA
>Z4 Y1 W1"),header = TRUE,stringsAsFactors=FALSE)
>
>The desired out put would be
>
>  Col1 Col2 col3
>1    X1    0    0
>2    K1    0    0
>3    Y1    0    0
>4    W1    0    0
>6    K2   X1    0
>7    Z1   K1   K2
>8    Z2    0    0
>9    Z3   X1    0
>10   Z4   Y1   W1
>
>K2 is already is already in col1 and should not be added.
>
>Thank you in advance
>
>
>
>
>
>
>
>On Sat, Feb 24, 2018 at 6:38 PM, Jim Lemon <[hidden email]>
>wrote:
>
>> Hi Val,
>> My fault - I assumed that the NA would be first in the result
>produced
>> by "unique":
>>
>> mydat <- read.table(textConnection("Col1 Col2 col3
>> Z1 K1 K2
>> Z2 NA NA
>> Z3 X1 NA
>> Z4 Y1 W1"),header = TRUE,stringsAsFactors=FALSE)
>> val23<-unique(unlist(mydat[,c("Col2","col3")]))
>> napos<-which(is.na(val23))
>> preval<-data.frame(Col1=val23[-napos],
>>  Col2=NA,col3=NA)
>> mydat<-rbind(preval,mydat)
>> mydat[is.na(mydat)]<-"0"
>> mydat
>>
>> Jim
>>
>> On Sun, Feb 25, 2018 at 11:27 AM, Val <[hidden email]> wrote:
>> > Thank you Jim,
>> >
>> > I read the data as you suggested but I could not find K1 in   col1.
>> >
>> > rbind(preval,mydat)
>> >   Col1 Col2 col3
>> > 1 <NA> <NA> <NA>
>> > 2   X1 <NA> <NA>
>> > 3   Y1 <NA> <NA>
>> > 4   K2 <NA> <NA>
>> > 5   W1 <NA> <NA>
>> > 6   Z1   K1   K2
>> > 7   Z2 <NA> <NA>
>> > 8   Z3   X1 <NA>
>> > 9   Z4   Y1   W1
>> >
>> >
>> >
>> > On Sat, Feb 24, 2018 at 6:18 PM, Jim Lemon <[hidden email]>
>wrote:
>> >>
>> >> hi Val,
>> >> Your problem seems to be that the data are read in as a factor.
>The
>> >> simplest way I can think of to get around this is:
>> >>
>> >> mydat <- read.table(textConnection("Col1 Col2 col3
>> >> Z1 K1 K2
>> >> Z2 NA NA
>> >> Z3 X1 NA
>> >> Z4 Y1 W1"),header = TRUE,stringsAsFactors=FALSE)
>> >>
>preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>> >>  Col2=NA,col3=NA)
>> >> rbind(preval,mydat)
>> >> mydat[is.na(mydat)]<-"0"
>> >>
>> >> Jiim
>> >>
>> >>
>> >> On Sun, Feb 25, 2018 at 11:05 AM, Val <[hidden email]> wrote:
>> >> > Sorry , I hit the send key accidentally  here is my complete
>message.
>> >> >
>> >> > Thank you Jim   and all, I got it.
>> >> >
>> >> > I have one more question on the original question
>> >> >
>> >> >  What does this  "[-1] "  do?
>> >> >
>preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>> >> >                    Col2=NA,col3=NA)
>> >> >
>> >> >
>> >> > mydat <- read.table(textConnection("Col1 Col2 col3
>> >> > Z1 K1 K2
>> >> > Z2 NA NA
>> >> > Z3 X1 NA
>> >> > Z4 Y1 W1"),header = TRUE)
>> >> >
>> >> >
>preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
>> >> >                    Col2=NA,col3=NA)
>> >> > rbind(unique(preval),mydat)
>> >> >
>> >> >
>> >> >  Col1 Col2 col3
>> >> > 1 <NA> <NA> <NA>
>> >> > 2   X1 <NA> <NA>
>> >> > 3   Y1 <NA> <NA>
>> >> > 4   K2 <NA> <NA>
>> >> > 5   W1 <NA> <NA>
>> >> > 6   Z1   K1   K2
>> >> > 7   Z2 <NA> <NA>
>> >> > 8   Z3   X1 <NA>
>> >> > 9   Z4   Y1   W1
>> >> >
>> >> > I could not find K1 in the first   col1. Is that possible to fix
>this?
>> >> >
>> >> > On Sat, Feb 24, 2018 at 5:59 PM, Val <[hidden email]> wrote:
>> >> >
>> >> >> Thank you Jim   and all, I got it.
>> >> >>
>> >> >> I have one more question on the original question
>> >> >>
>> >> >>  What does this  "[-1] "  do?
>> >> >> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2",
>> "col3")]))[-1],
>> >> >>                    Col2=NA,col3=NA)
>> >> >>
>> >> >>
>> >> >> mydat <- read.table(textConnection("Col1 Col2 col3
>> >> >> Z1 K1 K2
>> >> >> Z2 NA NA
>> >> >> Z3 X1 NA
>> >> >> Z4 Y1 W1"),header = TRUE)
>> >> >>
>> >> >> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2",
>> "col3")]))[-1],
>> >> >>                    Col2=NA,col3=NA)
>> >> >> rbind(unique(preval),mydat)
>> >> >>
>> >> >>
>> >> >>  Col1 Col2 col3
>> >> >> 1 <NA> <NA> <NA>
>> >> >> 2   X1 <NA> <NA>
>> >> >> 3   Y1 <NA> <NA>
>> >> >> 4   K2 <NA> <NA>
>> >> >> 5   W1 <NA> <NA>
>> >> >> 6   Z1   K1   K2
>> >> >> 7   Z2 <NA> <NA>
>> >> >> 8   Z3   X1 <NA>
>> >> >> 9   Z4   Y1   W1
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >> On Sat, Feb 24, 2018 at 5:04 PM, Duncan Murdoch
>> >> >> <[hidden email]>
>> >> >> wrote:
>> >> >>
>> >> >>> On 24/02/2018 1:53 PM, William Dunlap via R-help wrote:
>> >> >>>
>> >> >>>>     x1 =  rbind(unique(preval),mydat)
>> >> >>>>    x2 <- x1[is.na(x1)] <- 0
>> >> >>>>    x2  # gives 0
>> >> >>>>
>> >> >>>> Why introduce the 'x2'?   x1[...] <- 0 alters x1 in place and
>I
>> think
>> >> >>>> that
>> >> >>>> altered x1 is what you want.
>> >> >>>>
>> >> >>>> You asked why x2 was zero.  The value of the expression
>> >> >>>>     f(a) <- b
>> >> >>>> and assignments are processed right to left so
>> >> >>>>     x2 <- x[!is.na(x1)] <- 0
>> >> >>>> is equivalent to
>> >> >>>>     x[!is.na(x1)] <- 0
>> >> >>>>     x2 <- 0
>> >> >>>>
>> >> >>>
>> >> >>> That's not right in general, is it?  I'd think that should be
>> >> >>>
>> >> >>>     x[!is.na(x1)] <- 0
>> >> >>>     x2 <- x1
>> >> >>>
>> >> >>> Of course, in this example, x1 is 0, so it gives the same
>answer.
>> >> >>>
>> >> >>> Duncan Murdoch
>> >> >>>
>> >> >>>
>> >> >>>
>> >> >>>>
>> >> >>>> Bill Dunlap
>> >> >>>> TIBCO Software
>> >> >>>> wdunlap tibco.com
>> >> >>>>
>> >> >>>> On Sat, Feb 24, 2018 at 9:59 AM, Val <[hidden email]>
>wrote:
>> >> >>>>
>> >> >>>> Thank you Jim
>> >> >>>>>
>> >> >>>>> I wanted a final data frame  after replacing the NA's to "0"
>> >> >>>>>
>> >> >>>>> x1 =  rbind(unique(preval),mydat)
>> >> >>>>> x2 <- x1[is.na(x1)] <- 0
>> >> >>>>> x2
>> >> >>>>>   but I got this,
>> >> >>>>>
>> >> >>>>> [1] 0
>> >> >>>>>
>> >> >>>>> why I am getting this?
>> >> >>>>>
>> >> >>>>>
>> >> >>>>> On Sat, Feb 24, 2018 at 12:17 AM, Jim Lemon
><[hidden email]
>> >
>> >> >>>>> wrote:
>> >> >>>>>
>> >> >>>>> Hi Val,
>> >> >>>>>> Try this:
>> >> >>>>>>
>> >> >>>>>>
>> >> >>>>>> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2",
>> "col3")]))[-1],
>> >> >>>>>>   Col2=NA,col3=NA)
>> >> >>>>>> rbind(preval,mydat)
>> >> >>>>>>
>> >> >>>>>> Jim
>> >> >>>>>>
>> >> >>>>>> On Sat, Feb 24, 2018 at 3:34 PM, Val <[hidden email]>
>wrote:
>> >> >>>>>>
>> >> >>>>>>> Hi All,
>> >> >>>>>>>
>> >> >>>>>>> I am reading a file as follow,
>> >> >>>>>>>
>> >> >>>>>>> mydat <- read.table(textConnection("Col1 Col2 col3
>> >> >>>>>>> Z2 NA NA
>> >> >>>>>>> Z3 X1 NA
>> >> >>>>>>> Z4 Y1 W1"),header = TRUE)
>> >> >>>>>>>
>> >> >>>>>>> 1. "NA" are   missing  should be replace by 0
>> >> >>>>>>> 2.  value that are in COl2 and Col3  should be included
>in col1
>> >> >>>>>>> before
>> >> >>>>>>> they appear
>> >> >>>>>>> in col2 and col3. So the output data looks like as follow,
>> >> >>>>>>>
>> >> >>>>>>> X1  0  0
>> >> >>>>>>> Y1  0  0
>> >> >>>>>>> W1  0  0
>> >> >>>>>>> Z2  0  0
>> >> >>>>>>> Z3 X1  0
>> >> >>>>>>> Z4 Y1 W1
>> >> >>>>>>>
>> >> >>>>>>> Thank you in advance
>> >> >>>>>>>
>> >> >>>>>>>          [[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.
>> >> >>>>>>>
>> >> >>>>>>
>> >> >>>>>>
>> >> >>>>>          [[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.
>> >> >>>>>
>> >> >>>>>
>> >> >>>>         [[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/posti
>> >> >>>> ng-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.
>> >
>> >
>>
>
> [[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.
Reply | Threaded
Open this post in threaded view
|

Re: include

Val-17
Thank you all for your help and sorry for that.

On Sun, Feb 25, 2018 at 12:18 PM, Jeff Newmiller <[hidden email]>
wrote:

> Jim has been exceedingly patient (and may well continue to be so), but
> this smells like "failure to launch". At what point will you start showing
> your (failed) attempts at solving your own problems so we can help you work
> on your specific weaknesses and become self-sufficient?
> --
> Sent from my phone. Please excuse my brevity.
>
> On February 25, 2018 7:55:55 AM PST, Val <[hidden email]> wrote:
> >HI Jim and all,
> >
> >I want to put one more condition.   Include col2 and col3 if they are
> >not
> >in col1.
> >
> >Here is the data
> >mydat <- read.table(textConnection("Col1 Col2 col3
> >K2 X1 NA
> >Z1 K1 K2
> >Z2 NA NA
> >Z3 X1 NA
> >Z4 Y1 W1"),header = TRUE,stringsAsFactors=FALSE)
> >
> >The desired out put would be
> >
> >  Col1 Col2 col3
> >1    X1    0    0
> >2    K1    0    0
> >3    Y1    0    0
> >4    W1    0    0
> >6    K2   X1    0
> >7    Z1   K1   K2
> >8    Z2    0    0
> >9    Z3   X1    0
> >10   Z4   Y1   W1
> >
> >K2 is already is already in col1 and should not be added.
> >
> >Thank you in advance
> >
> >
> >
> >
> >
> >
> >
> >On Sat, Feb 24, 2018 at 6:38 PM, Jim Lemon <[hidden email]>
> >wrote:
> >
> >> Hi Val,
> >> My fault - I assumed that the NA would be first in the result
> >produced
> >> by "unique":
> >>
> >> mydat <- read.table(textConnection("Col1 Col2 col3
> >> Z1 K1 K2
> >> Z2 NA NA
> >> Z3 X1 NA
> >> Z4 Y1 W1"),header = TRUE,stringsAsFactors=FALSE)
> >> val23<-unique(unlist(mydat[,c("Col2","col3")]))
> >> napos<-which(is.na(val23))
> >> preval<-data.frame(Col1=val23[-napos],
> >>  Col2=NA,col3=NA)
> >> mydat<-rbind(preval,mydat)
> >> mydat[is.na(mydat)]<-"0"
> >> mydat
> >>
> >> Jim
> >>
> >> On Sun, Feb 25, 2018 at 11:27 AM, Val <[hidden email]> wrote:
> >> > Thank you Jim,
> >> >
> >> > I read the data as you suggested but I could not find K1 in   col1.
> >> >
> >> > rbind(preval,mydat)
> >> >   Col1 Col2 col3
> >> > 1 <NA> <NA> <NA>
> >> > 2   X1 <NA> <NA>
> >> > 3   Y1 <NA> <NA>
> >> > 4   K2 <NA> <NA>
> >> > 5   W1 <NA> <NA>
> >> > 6   Z1   K1   K2
> >> > 7   Z2 <NA> <NA>
> >> > 8   Z3   X1 <NA>
> >> > 9   Z4   Y1   W1
> >> >
> >> >
> >> >
> >> > On Sat, Feb 24, 2018 at 6:18 PM, Jim Lemon <[hidden email]>
> >wrote:
> >> >>
> >> >> hi Val,
> >> >> Your problem seems to be that the data are read in as a factor.
> >The
> >> >> simplest way I can think of to get around this is:
> >> >>
> >> >> mydat <- read.table(textConnection("Col1 Col2 col3
> >> >> Z1 K1 K2
> >> >> Z2 NA NA
> >> >> Z3 X1 NA
> >> >> Z4 Y1 W1"),header = TRUE,stringsAsFactors=FALSE)
> >> >>
> >preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
> >> >>  Col2=NA,col3=NA)
> >> >> rbind(preval,mydat)
> >> >> mydat[is.na(mydat)]<-"0"
> >> >>
> >> >> Jiim
> >> >>
> >> >>
> >> >> On Sun, Feb 25, 2018 at 11:05 AM, Val <[hidden email]> wrote:
> >> >> > Sorry , I hit the send key accidentally  here is my complete
> >message.
> >> >> >
> >> >> > Thank you Jim   and all, I got it.
> >> >> >
> >> >> > I have one more question on the original question
> >> >> >
> >> >> >  What does this  "[-1] "  do?
> >> >> >
> >preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
> >> >> >                    Col2=NA,col3=NA)
> >> >> >
> >> >> >
> >> >> > mydat <- read.table(textConnection("Col1 Col2 col3
> >> >> > Z1 K1 K2
> >> >> > Z2 NA NA
> >> >> > Z3 X1 NA
> >> >> > Z4 Y1 W1"),header = TRUE)
> >> >> >
> >> >> >
> >preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2","col3")]))[-1],
> >> >> >                    Col2=NA,col3=NA)
> >> >> > rbind(unique(preval),mydat)
> >> >> >
> >> >> >
> >> >> >  Col1 Col2 col3
> >> >> > 1 <NA> <NA> <NA>
> >> >> > 2   X1 <NA> <NA>
> >> >> > 3   Y1 <NA> <NA>
> >> >> > 4   K2 <NA> <NA>
> >> >> > 5   W1 <NA> <NA>
> >> >> > 6   Z1   K1   K2
> >> >> > 7   Z2 <NA> <NA>
> >> >> > 8   Z3   X1 <NA>
> >> >> > 9   Z4   Y1   W1
> >> >> >
> >> >> > I could not find K1 in the first   col1. Is that possible to fix
> >this?
> >> >> >
> >> >> > On Sat, Feb 24, 2018 at 5:59 PM, Val <[hidden email]> wrote:
> >> >> >
> >> >> >> Thank you Jim   and all, I got it.
> >> >> >>
> >> >> >> I have one more question on the original question
> >> >> >>
> >> >> >>  What does this  "[-1] "  do?
> >> >> >> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2",
> >> "col3")]))[-1],
> >> >> >>                    Col2=NA,col3=NA)
> >> >> >>
> >> >> >>
> >> >> >> mydat <- read.table(textConnection("Col1 Col2 col3
> >> >> >> Z1 K1 K2
> >> >> >> Z2 NA NA
> >> >> >> Z3 X1 NA
> >> >> >> Z4 Y1 W1"),header = TRUE)
> >> >> >>
> >> >> >> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2",
> >> "col3")]))[-1],
> >> >> >>                    Col2=NA,col3=NA)
> >> >> >> rbind(unique(preval),mydat)
> >> >> >>
> >> >> >>
> >> >> >>  Col1 Col2 col3
> >> >> >> 1 <NA> <NA> <NA>
> >> >> >> 2   X1 <NA> <NA>
> >> >> >> 3   Y1 <NA> <NA>
> >> >> >> 4   K2 <NA> <NA>
> >> >> >> 5   W1 <NA> <NA>
> >> >> >> 6   Z1   K1   K2
> >> >> >> 7   Z2 <NA> <NA>
> >> >> >> 8   Z3   X1 <NA>
> >> >> >> 9   Z4   Y1   W1
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >> On Sat, Feb 24, 2018 at 5:04 PM, Duncan Murdoch
> >> >> >> <[hidden email]>
> >> >> >> wrote:
> >> >> >>
> >> >> >>> On 24/02/2018 1:53 PM, William Dunlap via R-help wrote:
> >> >> >>>
> >> >> >>>>     x1 =  rbind(unique(preval),mydat)
> >> >> >>>>    x2 <- x1[is.na(x1)] <- 0
> >> >> >>>>    x2  # gives 0
> >> >> >>>>
> >> >> >>>> Why introduce the 'x2'?   x1[...] <- 0 alters x1 in place and
> >I
> >> think
> >> >> >>>> that
> >> >> >>>> altered x1 is what you want.
> >> >> >>>>
> >> >> >>>> You asked why x2 was zero.  The value of the expression
> >> >> >>>>     f(a) <- b
> >> >> >>>> and assignments are processed right to left so
> >> >> >>>>     x2 <- x[!is.na(x1)] <- 0
> >> >> >>>> is equivalent to
> >> >> >>>>     x[!is.na(x1)] <- 0
> >> >> >>>>     x2 <- 0
> >> >> >>>>
> >> >> >>>
> >> >> >>> That's not right in general, is it?  I'd think that should be
> >> >> >>>
> >> >> >>>     x[!is.na(x1)] <- 0
> >> >> >>>     x2 <- x1
> >> >> >>>
> >> >> >>> Of course, in this example, x1 is 0, so it gives the same
> >answer.
> >> >> >>>
> >> >> >>> Duncan Murdoch
> >> >> >>>
> >> >> >>>
> >> >> >>>
> >> >> >>>>
> >> >> >>>> Bill Dunlap
> >> >> >>>> TIBCO Software
> >> >> >>>> wdunlap tibco.com
> >> >> >>>>
> >> >> >>>> On Sat, Feb 24, 2018 at 9:59 AM, Val <[hidden email]>
> >wrote:
> >> >> >>>>
> >> >> >>>> Thank you Jim
> >> >> >>>>>
> >> >> >>>>> I wanted a final data frame  after replacing the NA's to "0"
> >> >> >>>>>
> >> >> >>>>> x1 =  rbind(unique(preval),mydat)
> >> >> >>>>> x2 <- x1[is.na(x1)] <- 0
> >> >> >>>>> x2
> >> >> >>>>>   but I got this,
> >> >> >>>>>
> >> >> >>>>> [1] 0
> >> >> >>>>>
> >> >> >>>>> why I am getting this?
> >> >> >>>>>
> >> >> >>>>>
> >> >> >>>>> On Sat, Feb 24, 2018 at 12:17 AM, Jim Lemon
> ><[hidden email]
> >> >
> >> >> >>>>> wrote:
> >> >> >>>>>
> >> >> >>>>> Hi Val,
> >> >> >>>>>> Try this:
> >> >> >>>>>>
> >> >> >>>>>>
> >> >> >>>>>> preval<-data.frame(Col1=unique(unlist(mydat[,c("Col2",
> >> "col3")]))[-1],
> >> >> >>>>>>   Col2=NA,col3=NA)
> >> >> >>>>>> rbind(preval,mydat)
> >> >> >>>>>>
> >> >> >>>>>> Jim
> >> >> >>>>>>
> >> >> >>>>>> On Sat, Feb 24, 2018 at 3:34 PM, Val <[hidden email]>
> >wrote:
> >> >> >>>>>>
> >> >> >>>>>>> Hi All,
> >> >> >>>>>>>
> >> >> >>>>>>> I am reading a file as follow,
> >> >> >>>>>>>
> >> >> >>>>>>> mydat <- read.table(textConnection("Col1 Col2 col3
> >> >> >>>>>>> Z2 NA NA
> >> >> >>>>>>> Z3 X1 NA
> >> >> >>>>>>> Z4 Y1 W1"),header = TRUE)
> >> >> >>>>>>>
> >> >> >>>>>>> 1. "NA" are   missing  should be replace by 0
> >> >> >>>>>>> 2.  value that are in COl2 and Col3  should be included
> >in col1
> >> >> >>>>>>> before
> >> >> >>>>>>> they appear
> >> >> >>>>>>> in col2 and col3. So the output data looks like as follow,
> >> >> >>>>>>>
> >> >> >>>>>>> X1  0  0
> >> >> >>>>>>> Y1  0  0
> >> >> >>>>>>> W1  0  0
> >> >> >>>>>>> Z2  0  0
> >> >> >>>>>>> Z3 X1  0
> >> >> >>>>>>> Z4 Y1 W1
> >> >> >>>>>>>
> >> >> >>>>>>> Thank you in advance
> >> >> >>>>>>>
> >> >> >>>>>>>          [[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.
> >> >> >>>>>>>
> >> >> >>>>>>
> >> >> >>>>>>
> >> >> >>>>>          [[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.
> >> >> >>>>>
> >> >> >>>>>
> >> >> >>>>         [[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/posti
> >> >> >>>> ng-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.
> >> >
> >> >
> >>
> >
> >       [[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.
>

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