Re: [R] Extracting part of a factor

2016-03-04 Thread Ista Zahn
I guess this thread has gone on long enough, but I haven't seen anyone
yet suggest what to me seems like the obvious thing if you want to do
this with mutate, namely

testdata <- mutate(testdata, place = as.factor(substr(subject, 1, 3)))

Best,
Ista

On Fri, Mar 4, 2016 at 10:45 PM, Boris Steipe  wrote:
> You mean this?
>   test$place <- factor(test$place)
>
> You can create a new column in a data frame by assigning something to it. E.g.
>test$pollywog <- 1:6
> ... creates that column in "test".
>
> But factor(test$place) was empty, because no such column previously existed, 
> like:
> R > factor(test$barbapapa)
> factor(0)
> Levels:
>
> So the right hand side has 0 rows, but the left hand side needs six. Of 
> course you could create your column directly:
>
> R > str(test)
> 'data.frame':   6 obs. of  6 variables:
>  $ subject: Factor w/ 6 levels "001-002","002-003",..: 1 2 3 4 5 6
>  $ group  : Factor w/ 2 levels "boys","girls": 1 1 1 2 2 2
>  $ wk1: int  2 7 9 5 2 1
>  $ wk2: int  3 6 4 7 6 4
>  $ wk3: int  4 5 6 8 3 7
>  $ wk4: int  5 4 1 9 8 4
> R > test$place <- factor(substr(test$subject,1,3))# here's were it gets 
> done
> R > str(test)
> 'data.frame':   6 obs. of  7 variables:
>  $ subject: Factor w/ 6 levels "001-002","002-003",..: 1 2 3 4 5 6
>  $ group  : Factor w/ 2 levels "boys","girls": 1 1 1 2 2 2
>  $ wk1: int  2 7 9 5 2 1
>  $ wk2: int  3 6 4 7 6 4
>  $ wk3: int  4 5 6 8 3 7
>  $ wk4: int  5 4 1 9 8 4
>  $ place  : Factor w/ 6 levels "001","002","003",..: 1 2 3 4 5 6
>
> ... it's just that you insisted on mutate().
>
>
>
> Cheers,
> Boris
>
>
> On Mar 4, 2016, at 9:31 PM, KMNanus  wrote:
>
>> Boris -
>>
>> Boy, do I feel dumb - that’s exactly what I wanted.  I’ve tried this every 
>> way I can think of without assigning the result to the original name of the 
>> data frame.  I was trying to assign the result to a variable (test$place).
>>
>> Can u pls explain to me why assigning the result to the new variable was 
>> wrong?
>>
>> BTW, really appreciate your help.
>>
>> Ken
>> kmna...@gmail.com
>> 914-450-0816 (tel)
>> 347-730-4813 (fax)
>>
>> 
>>
>>> On Mar 4, 2016, at 9:21 PM, Boris Steipe  wrote:
>>>
>>> LOL you still need to assign it though:
>>>
>>>
>>> test <- mutate(test, place = factor(substr(test$subject,1,3)))
>>>
>>> str(test)
>>> 'data.frame':6 obs. of  7 variables:
>>> $ subject: Factor w/ 6 levels "001-002","002-003",..: 1 2 3 4 5 6
>>> $ group  : Factor w/ 2 levels "boys","girls": 1 1 1 2 2 2
>>> $ wk1: int  2 7 9 5 2 1
>>> $ wk2: int  3 6 4 7 6 4
>>> $ wk3: int  4 5 6 8 3 7
>>> $ wk4: int  5 4 1 9 8 4
>>> $ place  : Factor w/ 6 levels "001","002","003",..: 1 2 3 4 5 6
>>>
>>>
>>> Without assigning the result, the output only gets printed to console. 
>>> Remember that R is a functional language - a properly written R functio 
>>> does not change anything, it only returns its result.
>>>
>>> :-)
>>>
>>>
>>> On Mar 4, 2016, at 4:13 PM, KMNanus  wrote:
>>>
 If I call mutate this way - mutate(test, place = 
 factor(substr(test$subject,1,3))), I get the same output as above but when 
 I call class(test$place), I get NULL and the variable disappears.
>>>
>>
>
> __
> R-help@r-project.org 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.

__
R-help@r-project.org 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.

Re: [R] Extracting part of a factor

2016-03-04 Thread Boris Steipe
You mean this?
  test$place <- factor(test$place)

You can create a new column in a data frame by assigning something to it. E.g. 
   test$pollywog <- 1:6
... creates that column in "test".

But factor(test$place) was empty, because no such column previously existed, 
like:
R > factor(test$barbapapa)
factor(0)
Levels: 

So the right hand side has 0 rows, but the left hand side needs six. Of course 
you could create your column directly:

R > str(test)
'data.frame':   6 obs. of  6 variables:
 $ subject: Factor w/ 6 levels "001-002","002-003",..: 1 2 3 4 5 6
 $ group  : Factor w/ 2 levels "boys","girls": 1 1 1 2 2 2
 $ wk1: int  2 7 9 5 2 1
 $ wk2: int  3 6 4 7 6 4
 $ wk3: int  4 5 6 8 3 7
 $ wk4: int  5 4 1 9 8 4
R > test$place <- factor(substr(test$subject,1,3))# here's were it gets done
R > str(test)
'data.frame':   6 obs. of  7 variables:
 $ subject: Factor w/ 6 levels "001-002","002-003",..: 1 2 3 4 5 6
 $ group  : Factor w/ 2 levels "boys","girls": 1 1 1 2 2 2
 $ wk1: int  2 7 9 5 2 1
 $ wk2: int  3 6 4 7 6 4
 $ wk3: int  4 5 6 8 3 7
 $ wk4: int  5 4 1 9 8 4
 $ place  : Factor w/ 6 levels "001","002","003",..: 1 2 3 4 5 6

... it's just that you insisted on mutate().



Cheers,
Boris


On Mar 4, 2016, at 9:31 PM, KMNanus  wrote:

> Boris - 
> 
> Boy, do I feel dumb - that’s exactly what I wanted.  I’ve tried this every 
> way I can think of without assigning the result to the original name of the 
> data frame.  I was trying to assign the result to a variable (test$place).
> 
> Can u pls explain to me why assigning the result to the new variable was 
> wrong?
> 
> BTW, really appreciate your help.
>  
> Ken
> kmna...@gmail.com
> 914-450-0816 (tel)
> 347-730-4813 (fax)
> 
> 
> 
>> On Mar 4, 2016, at 9:21 PM, Boris Steipe  wrote:
>> 
>> LOL you still need to assign it though:
>> 
>> 
>> test <- mutate(test, place = factor(substr(test$subject,1,3)))
>> 
>> str(test)
>> 'data.frame':6 obs. of  7 variables:
>> $ subject: Factor w/ 6 levels "001-002","002-003",..: 1 2 3 4 5 6
>> $ group  : Factor w/ 2 levels "boys","girls": 1 1 1 2 2 2
>> $ wk1: int  2 7 9 5 2 1
>> $ wk2: int  3 6 4 7 6 4
>> $ wk3: int  4 5 6 8 3 7
>> $ wk4: int  5 4 1 9 8 4
>> $ place  : Factor w/ 6 levels "001","002","003",..: 1 2 3 4 5 6
>> 
>> 
>> Without assigning the result, the output only gets printed to console. 
>> Remember that R is a functional language - a properly written R functio does 
>> not change anything, it only returns its result.
>> 
>> :-)
>> 
>> 
>> On Mar 4, 2016, at 4:13 PM, KMNanus  wrote:
>> 
>>> If I call mutate this way - mutate(test, place = 
>>> factor(substr(test$subject,1,3))), I get the same output as above but when 
>>> I call class(test$place), I get NULL and the variable disappears.
>> 
> 

__
R-help@r-project.org 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.


Re: [R] Extracting part of a factor

2016-03-04 Thread Boris Steipe
LOL you still need to assign it though:


test <- mutate(test, place = factor(substr(test$subject,1,3)))

str(test)
'data.frame':   6 obs. of  7 variables:
 $ subject: Factor w/ 6 levels "001-002","002-003",..: 1 2 3 4 5 6
 $ group  : Factor w/ 2 levels "boys","girls": 1 1 1 2 2 2
 $ wk1: int  2 7 9 5 2 1
 $ wk2: int  3 6 4 7 6 4
 $ wk3: int  4 5 6 8 3 7
 $ wk4: int  5 4 1 9 8 4
 $ place  : Factor w/ 6 levels "001","002","003",..: 1 2 3 4 5 6


Without assigning the result, the output only gets printed to console. Remember 
that R is a functional language - a properly written R functio does not change 
anything, it only returns its result.

:-)


On Mar 4, 2016, at 4:13 PM, KMNanus  wrote:

> If I call mutate this way - mutate(test, place = 
> factor(substr(test$subject,1,3))), I get the same output as above but when I 
> call class(test$place), I get NULL and the variable disappears.

__
R-help@r-project.org 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.


Re: [R] Extracting part of a factor

2016-03-04 Thread KMNanus
Here’s where I’m stumped - 

when I call mutate(test, place = substr(test$subject, 1,3)) to create a place 
variable, I get this, with place as a character variable. 

 subject  group   wk1   wk2   wk3   wk4 place
   (fctr) (fctr) (int) (int) (int) (int) (chr)
1 001-002   boys 2 3 4 5   001
2 002-003   boys 7 6 5 4   002
3 003-004   boys 9 4 6 1   003
4 004-005  girls 5 7 8 9   004
5 005-006  girls 2 6 3 8   005
6 006-007  girls 1 4 7 4   006

When I call test$place <- factor(test$place), I receive the msg  - "Error in 
`$<-.data.frame`(`*tmp*`, "place", value = integer(0)) : 
  replacement has 0 rows, data has 6.

If I call mutate this way - mutate(test, place = 
factor(substr(test$subject,1,3))), I get the same output as above but when I 
call class(test$place), I get NULL and the variable disappears.

I can’t figure out why.

Ken
kmna...@gmail.com
914-450-0816 (tel)
347-730-4813 (fax)



> On Mar 4, 2016, at 3:46 PM, Jeff Newmiller  wrote:
> 
> I much prefer the factor function over the as.factor function for converting 
> character to factor, since you can set the levels in the order you want them 
> to be. 
> -- 
> Sent from my phone. Please excuse my brevity.
> 
> On March 4, 2016 10:07:27 AM PST, Sarah Goslee  wrote:
> As everyone has been telling you, as.factor().
> If you like the mutate approach, you can call as.factor(test$subject)
> to convert it.
> 
> Here's a one-liner with reproducible data.
> 
> 
> testdata <- structure(list(subject = structure(1:6, .Label = c("001-002",
> "002-003", "003-004", "004-005", "005-006", "006-007"), class = "factor"),
> group = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("boys",
> "girls"), class = "factor"), wk1 = c(2L, 7L, 9L, 5L, 2L,
> 1L), wk2 = c(3L, 6L, 4L, 7L, 6L, 4L), wk3 = c(4L, 5L, 6L,
> 8L, 3L, 7L), wk4 = c(5L, 4L, 1L, 9L, 8L, 4L)), .Names = c("subject",
> "group", "wk1", "wk2", "wk3", "wk4"), class = "data.frame", row.names = c(NA,
> -6L))
> 
> testdata$subject <- as.factor(substring(as.character(testdata$subject), 1, 3))
> 
> 
> testdata
>   subject group wk1 wk2 wk3 wk4
> 1 001  boys   2   3   4   5
> 2 002  boys   7   6   5   4
> 3 003  boys   9   4   6   1
> 4 004 girls   5   7   8   9
> 5 005 girls   2   6   3   8
> 6 006 girls   1   4   7   4
>  str(testdata)
> 'data.frame': 6 obs. of  6 variables:
>  $ subject: Factor w/ 6 levels "001","002","003",..: 1 2 3 4 5 6
>  $ group  : Factor w/ 2 levels "boys","girls": 1 1 1 2 2 2
>  $ wk1: int  2 7 9 5 2 1
>  $ wk2: int  3 6 4 7 6 4
>  $ wk3: int  4 5 6 8 3 7
>  $ wk4: int  5 4 1 9 8 4
> 
> Sarah
> 
> On Fri, Mar 4, 2016 at 1:00 PM, KMNanus  wrote:
> 
>  Here’s the dataset
> I’m working with, called test -
> 
>  subject group wk1 wk2 wk3 wk4 place
>  001-002 boys 2 3 4 5
>  002-003 boys 7 6 5 4
>  003-004 boys 9 4 6 1
>  004-005 girls 5 7 8 9
>  005-006 girls 2 6 3 8
>  006-007 girls 1 4 7 4
> 
> 
>  if I call mutate(test, place = substr(subject,1,3), “001 is the first 
> observation in the place column
> 
>  But it’s a character and “subject” is a factor.  I need place to be a 
> factor, too, but I need the observations to be ONLY the first three numbers 
> of “subject.”
> 
>  Does that make my request more understandable?
> 
> 
> R-help@r-project.org 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.

__
R-help@r-project.org 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.

Re: [R] Extracting part of a factor

2016-03-04 Thread KMNanus
Here’s the dataset I’m working with, called test - 

subject group   wk1 wk2 wk3 wk4 place
001-002 boys2   3   4   5   
002-003 boys7   6   5   4   
003-004 boys9   4   6   1   
004-005 girls   5   7   8   9   
005-006 girls   2   6   3   8   
006-007 girls   1   4   7   4   


if I call mutate(test, place = substr(subject,1,3), “001 is the first 
observation in the place column

But it’s a character and “subject” is a factor.  I need place to be a factor, 
too, but I need the observations to be ONLY the first three numbers of 
“subject.”

Does that make my request more understandable?

Ken
kmna...@gmail.com
914-450-0816 (tel)
347-730-4813 (fax)



> On Mar 4, 2016, at 12:49 PM, Sarah Goslee  wrote:
> 
> Hi Ken,
> 
> You do that with as.factor(), as has already been suggested. You'll need to 
> provide a reproducible example to show us what's going wrong. Using fake data 
> is fine, we just need to see some data that look like yours and the code 
> you're using.
> 
> Sarah
> 
> On Fri, Mar 4, 2016 at 11:57 AM, KMNanus  > wrote:
> Let me see if I can ask the question more clearly - I am trying to extract a 
> section of a hyphenated factor. For example, 001-004 is one observation of 
> test$ken, which is a factor, and I want to set up a new factor variable 
> called place that would have 001 as an observation. If I call mutate(place = 
> (as.character (test$ken)), I can extract 001 from  001-004, but but don't 
> know how to subsequently convert that character string back into a factor.
> 
> 
> Or can 001 be extracted from a factor as a factor?
> 
> Do you know how to execute either of these approaches?
> 
> Ken
> kmna...@gmail.com 
> 914-450-0816  (tel)
> 347-730-4813  (fax)
> 
> 
> 
> > On Mar 3, 2016, at 8:33 PM, Hervé Pagès  > > wrote:
> >
> > On 03/03/2016 02:13 PM, KMNanus wrote:
> >> When I do that,
> >
> > When you do what exactly?
> >
> > It's impossible for anyone here to know what you're doing if you
> > don't show the code.
> >
> >> I get "Error in `$<-.data.frame`(`*tmp*`, "site", value
> >> = integer(0)) :
> >>   replacement has 0 rows, data has 6”
> >>
> >> The data frame has 6 rows.
> >
> > You said you had a factor variable, you never mentioned you had a
> > data.frame. If the factor variable is part of a data.frame 'df',
> > then first extract it with something like df$myvar or df[["myvar"]],
> > and then call substr() followed by as.factor() on it.
> >
> > H.
> >
> >>
> >> Ken
> >> kmna...@gmail.com   >> >
> >> 914-450-0816  (tel)
> >> 347-730-4813  (fax)
> >>
> >>
> >>> On Mar 3, 2016, at 4:52 PM, Hervé Pagès  >>> 
> >>> >> wrote:
> >>>
> >>> Hi,
> >>>
> >>> On 03/03/2016 12:18 PM, KMNanus wrote:
>  I have a factor variable that is 6 digits and hyphenated.  For
>  example, 001-014.
> 
>  I need to extract the first 3 digits to a new variable using mutate
>  in dplyr - in this case 001 - but can’t find a function to do it.
> 
>  substr will do this for character strings, but I need the variable to
>  remain as a factor.
> >>>
> >>> What prevents you from calling as.factor() on the result to turn it
> >>> back into a factor?
> >>>
> >>> H.
> >>>
> 
>  Is there an R function  or workaround to do this?
> 
> 
>  Ken
>  kmna...@gmail.com    >
>  914-450-0816  (tel)
>  347-730-4813  (fax)
> 
> 
> 
>  __
>  R-help@r-project.org  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.
> 
> >>>
> >>> --
> >>> Hervé Pagès
> >>>
> >>> Program in Computational Biology
> >>> Division of Public Health Sciences
> >>> Fred Hutchinson Cancer Research Center
> >>> 1100 Fairview Ave. N, M1-B514
> >>> P.O. Box 19024
> >>> Seattle, WA 98109-1024
> >>>
> >>> E-mail: hpa...@fredhutch.org  
> >>> >
> >>> Phone:  (206) 667-5791 
> >>> Fax:(206) 667-1319 
> >>
> >
> > --
> > Hervé Pagès
> >
> > Program in Computational Biology
> > Division of Public Health Sciences
> > Fred Hutchinson Cancer Research Center
> > 1100 Fairview Ave. N, M1-B514
> > P.O. Box 19024
> > Seattle, WA 98109-1024
> >
> > E-mail: h

Re: [R] Extracting part of a factor

2016-03-04 Thread Jeff Newmiller
This is not a problem with factor or as.factor, this is a problem with your use 
of the dplyr package or with bugs in that package. 

Please make a reproducible example, making sure to use the dput function to 
create the code that initializes the data so we can run your code. Read the 
Posting Guide for more on reproducibility.

To avoid getting your code messed up in transit to us, make sure you send your 
email in plain text mode... this is also mentioned in the PG.
-- 
Sent from my phone. Please excuse my brevity.

On March 4, 2016 1:13:54 PM PST, KMNanus  wrote:
>Here’s where I’m stumped - 
>
>when I call mutate(test, place = substr(test$subject, 1,3)) to create a
>place variable, I get this, with place as a character variable. 
>
> subject  group   wk1   wk2   wk3   wk4 place
>   (fctr) (fctr) (int) (int) (int) (int) (chr)
>1 001-002   boys 2 3 4 5   001
>2 002-003   boys 7 6 5 4   002
>3 003-004   boys 9 4 6 1   003
>4 004-005  girls 5 7 8 9   004
>5 005-006  girls 2 6 3 8   005
>6 006-007  girls 1 4 7 4   006
>
>When I call test$place <- factor(test$place), I receive the msg  -
>"Error in `$<-.data.frame`(`*tmp*`, "place", value = integer(0)) : 
>  replacement has 0 rows, data has 6.
>
>If I call mutate this way - mutate(test, place =
>factor(substr(test$subject,1,3))), I get the same output as above but
>when I call class(test$place), I get NULL and the variable disappears.
>
>I can’t figure out why.
>
>Ken
>kmna...@gmail.com
>914-450-0816 (tel)
>347-730-4813 (fax)
>
>
>
>> On Mar 4, 2016, at 3:46 PM, Jeff Newmiller 
>wrote:
>> 
>> I much prefer the factor function over the as.factor function for
>converting character to factor, since you can set the levels in the
>order you want them to be. 
>> -- 
>> Sent from my phone. Please excuse my brevity.
>> 
>> On March 4, 2016 10:07:27 AM PST, Sarah Goslee
> wrote:
>> As everyone has been telling you, as.factor().
>> If you like the mutate approach, you can call as.factor(test$subject)
>> to convert it.
>> 
>> Here's a one-liner with reproducible data.
>> 
>> 
>> testdata <- structure(list(subject = structure(1:6, .Label =
>c("001-002",
>> "002-003", "003-004", "004-005", "005-006", "006-007"), class =
>"factor"),
>> group = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("boys",
>> "girls"), class = "factor"), wk1 = c(2L, 7L, 9L, 5L, 2L,
>> 1L), wk2 = c(3L, 6L, 4L, 7L, 6L, 4L), wk3 = c(4L, 5L, 6L,
>> 8L, 3L, 7L), wk4 = c(5L, 4L, 1L, 9L, 8L, 4L)), .Names =
>c("subject",
>> "group", "wk1", "wk2", "wk3", "wk4"), class = "data.frame", row.names
>= c(NA,
>> -6L))
>> 
>> testdata$subject <-
>as.factor(substring(as.character(testdata$subject), 1, 3))
>> 
>> 
>> testdata
>>   subject group wk1 wk2 wk3 wk4
>> 1 001  boys   2   3   4   5
>> 2 002  boys   7   6   5   4
>> 3 003  boys   9   4   6   1
>> 4 004 girls   5   7   8   9
>> 5 005 girls   2   6   3   8
>> 6 006 girls   1   4   7   4
>>  str(testdata)
>> 'data.frame': 6 obs. of  6 variables:
>>  $ subject: Factor w/ 6 levels "001","002","003",..: 1 2 3 4 5 6
>>  $ group  : Factor w/ 2 levels "boys","girls": 1 1 1 2 2 2
>>  $ wk1: int  2 7 9 5 2 1
>>  $ wk2: int  3 6 4 7 6 4
>>  $ wk3: int  4 5 6 8 3 7
>>  $ wk4: int  5 4 1 9 8 4
>> 
>> Sarah
>> 
>> On Fri, Mar 4, 2016 at 1:00 PM, KMNanus  wrote:
>> 
>>  Here’s the dataset
>> I’m working with, called test -
>> 
>>  subject group wk1 wk2 wk3 wk4 place
>>  001-002 boys 2 3 4 5
>>  002-003 boys 7 6 5 4
>>  003-004 boys 9 4 6 1
>>  004-005 girls 5 7 8 9
>>  005-006 girls 2 6 3 8
>>  006-007 girls 1 4 7 4
>> 
>> 
>>  if I call mutate(test, place = substr(subject,1,3), “001 is the
>first observation in the place column
>> 
>>  But it’s a character and “subject” is a factor.  I need place to be
>a factor, too, but I need the observations to be ONLY the first three
>numbers of “subject.”
>> 
>>  Does that make my request more understandable?
>> 
>> 
>> R-help@r-project.org 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]]

__
R-help@r-project.org 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.

Re: [R] Extracting part of a factor

2016-03-04 Thread Sarah Goslee
You're not saving the result of mutate(). You're just printing it to the screen.

Try instead:
test <- mutate(testdata, place = substr(testdata$subject, 1,3))
test$place <- as.factor(test$place) # or factor() if you'd rather

This is why we ask for reproducible examples with data and code.
Look through the following and see if you understand.


test <- structure(list(subject = structure(1:6, .Label = c("001-002",
"002-003", "003-004", "004-005", "005-006", "006-007"), class = "factor"),
group = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("boys",
"girls"), class = "factor"), wk1 = c(2L, 7L, 9L, 5L, 2L,
1L), wk2 = c(3L, 6L, 4L, 7L, 6L, 4L), wk3 = c(4L, 5L, 6L,
8L, 3L, 7L), wk4 = c(5L, 4L, 1L, 9L, 8L, 4L)), .Names = c("subject",
"group", "wk1", "wk2", "wk3", "wk4"), class = "data.frame", row.names = c(NA,
-6L))

> str(test)
'data.frame': 6 obs. of  6 variables:
 $ subject: Factor w/ 6 levels "001-002","002-003",..: 1 2 3 4 5 6
 $ group  : Factor w/ 2 levels "boys","girls": 1 1 1 2 2 2
 $ wk1: int  2 7 9 5 2 1
 $ wk2: int  3 6 4 7 6 4
 $ wk3: int  4 5 6 8 3 7
 $ wk4: int  5 4 1 9 8 4
> mutate(test, place = substr(testdata$subject, 1,3))
  subject group wk1 wk2 wk3 wk4 place
1 001-002  boys   2   3   4   5   001
2 002-003  boys   7   6   5   4   002
3 003-004  boys   9   4   6   1   003
4 004-005 girls   5   7   8   9   004
5 005-006 girls   2   6   3   8   005
6 006-007 girls   1   4   7   4   006
> str(test)
'data.frame': 6 obs. of  6 variables:
 $ subject: Factor w/ 6 levels "001-002","002-003",..: 1 2 3 4 5 6
 $ group  : Factor w/ 2 levels "boys","girls": 1 1 1 2 2 2
 $ wk1: int  2 7 9 5 2 1
 $ wk2: int  3 6 4 7 6 4
 $ wk3: int  4 5 6 8 3 7
 $ wk4: int  5 4 1 9 8 4



test <- mutate(testdata, place = substr(testdata$subject, 1,3))
test$place <- as.factor(test$place)

> str(test)
'data.frame': 6 obs. of  7 variables:
 $ subject: Factor w/ 6 levels "001-002","002-003",..: 1 2 3 4 5 6
 $ group  : Factor w/ 2 levels "boys","girls": 1 1 1 2 2 2
 $ wk1: int  2 7 9 5 2 1
 $ wk2: int  3 6 4 7 6 4
 $ wk3: int  4 5 6 8 3 7
 $ wk4: int  5 4 1 9 8 4
 $ place  : Factor w/ 6 levels "001","002","003",..: 1 2 3 4 5 6



On Fri, Mar 4, 2016 at 4:13 PM, KMNanus  wrote:
> Here’s where I’m stumped -
>
> when I call mutate(test, place = substr(test$subject, 1,3)) to create a
> place variable, I get this, with place as a character variable.
>
>  subject  group   wk1   wk2   wk3   wk4 place
>(fctr) (fctr) (int) (int) (int) (int) (chr)
> 1 001-002   boys 2 3 4 5   001
> 2 002-003   boys 7 6 5 4   002
> 3 003-004   boys 9 4 6 1   003
> 4 004-005  girls 5 7 8 9   004
> 5 005-006  girls 2 6 3 8   005
> 6 006-007  girls 1 4 7 4   006
>
> When I call test$place <- factor(test$place), I receive the msg  - "Error in
> `$<-.data.frame`(`*tmp*`, "place", value = integer(0)) :
>   replacement has 0 rows, data has 6.
>
> If I call mutate this way - mutate(test, place =
> factor(substr(test$subject,1,3))), I get the same output as above but when I
> call class(test$place), I get NULL and the variable disappears.
>
> I can’t figure out why.
>
> Ken
> kmna...@gmail.com
> 914-450-0816 (tel)
> 347-730-4813 (fax)
>
>
> On Mar 4, 2016, at 3:46 PM, Jeff Newmiller  wrote:
>
> I much prefer the factor function over the as.factor function for converting
> character to factor, since you can set the levels in the order you want them
> to be.
> --
> Sent from my phone. Please excuse my brevity.
>
> On March 4, 2016 10:07:27 AM PST, Sarah Goslee 
> wrote:
>>
>> As everyone has been telling you, as.factor().
>> If you like the mutate approach, you can call as.factor(test$subject)
>> to convert it.
>>
>> Here's a one-liner with reproducible data.
>>
>>
>> testdata <- structure(list(subject = structure(1:6, .Label = c("001-002",
>> "002-003", "003-004", "004-005", "005-006", "006-007"), class = "factor"),
>> group = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("boys",
>> "girls"), class = "factor"), wk1 = c(2L, 7L, 9L, 5L, 2L,
>> 1L), wk2 = c(3L, 6L, 4L, 7L, 6L, 4L), wk3 = c(4L, 5L, 6L,
>> 8L, 3L, 7L), wk4 = c(5L, 4L, 1L, 9L, 8L, 4L)), .Names = c("subject",
>> "group", "wk1", "wk2", "wk3", "wk4"), class = "data.frame", row.names =
>> c(NA,
>> -6L))
>>
>> testdata$subject <- as.factor(substring(as.character(testdata$subject), 1,
>> 3))
>>
>>>
>>> testdata
>>
>>   subject group wk1 wk2 wk3 wk4
>> 1 001  boys   2   3   4   5
>> 2 002  boys   7   6   5   4
>> 3 003  boys   9   4   6   1
>> 4 004 girls   5   7   8   9
>> 5 005 girls   2   6   3   8
>> 6 006 girls   1   4   7   4
>>>
>>>  str(testdata)
>>
>> 'data.frame': 6 obs. of  6 variables:
>>  $ subject: Factor w/ 6 levels "001","002","003",..: 1 2 3 4 5 6
>>  $ group  : Factor w/ 2 levels "boys","girls": 1 1 1 2 2 2
>>  $ wk1: int  2 7 9 5 2 1
>>  $ wk2: int  3 6 4 7 6 4
>>  $ wk3: int  4 

Re: [R] Extracting part of a factor

2016-03-04 Thread Jeff Newmiller
I much prefer the factor function over the as.factor function for converting 
character to factor, since you can set the levels in the order you want them to 
be. 
-- 
Sent from my phone. Please excuse my brevity.

On March 4, 2016 10:07:27 AM PST, Sarah Goslee  wrote:
>As everyone has been telling you, as.factor().
>If you like the mutate approach, you can call as.factor(test$subject)
>to convert it.
>
>Here's a one-liner with reproducible data.
>
>
>testdata <- structure(list(subject = structure(1:6, .Label =
>c("001-002",
>"002-003", "003-004", "004-005", "005-006", "006-007"), class =
>"factor"),
>group = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("boys",
>"girls"), class = "factor"), wk1 = c(2L, 7L, 9L, 5L, 2L,
>1L), wk2 = c(3L, 6L, 4L, 7L, 6L, 4L), wk3 = c(4L, 5L, 6L,
>   8L, 3L, 7L), wk4 = c(5L, 4L, 1L, 9L, 8L, 4L)), .Names = c("subject",
>"group", "wk1", "wk2", "wk3", "wk4"), class = "data.frame", row.names =
>c(NA,
>-6L))
>
>testdata$subject <- as.factor(substring(as.character(testdata$subject),
>1, 3))
>
>> testdata
>  subject group wk1 wk2 wk3 wk4
>1 001  boys   2   3   4   5
>2 002  boys   7   6   5   4
>3 003  boys   9   4   6   1
>4 004 girls   5   7   8   9
>5 005 girls   2   6   3   8
>6 006 girls   1   4   7   4
>> str(testdata)
>'data.frame': 6 obs. of  6 variables:
> $ subject: Factor w/ 6 levels "001","002","003",..: 1 2 3 4 5 6
> $ group  : Factor w/ 2 levels "boys","girls": 1 1 1 2 2 2
> $ wk1: int  2 7 9 5 2 1
> $ wk2: int  3 6 4 7 6 4
> $ wk3: int  4 5 6 8 3 7
> $ wk4: int  5 4 1 9 8 4
>
>Sarah
>
>On Fri, Mar 4, 2016 at 1:00 PM, KMNanus  wrote:
>>
>> Here’s the dataset I’m working with, called test -
>>
>> subject group wk1 wk2 wk3 wk4 place
>> 001-002 boys 2 3 4 5
>> 002-003 boys 7 6 5 4
>> 003-004 boys 9 4 6 1
>> 004-005 girls 5 7 8 9
>> 005-006 girls 2 6 3 8
>> 006-007 girls 1 4 7 4
>>
>>
>> if I call mutate(test, place = substr(subject,1,3), “001 is the first
>observation in the place column
>>
>> But it’s a character and “subject” is a factor.  I need place to be a
>factor, too, but I need the observations to be ONLY the first three
>numbers of “subject.”
>>
>> Does that make my request more understandable?
>
>__
>R-help@r-project.org 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]]

__
R-help@r-project.org 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.

Re: [R] Extracting part of a factor

2016-03-04 Thread Sarah Goslee
As everyone has been telling you, as.factor().
If you like the mutate approach, you can call as.factor(test$subject)
to convert it.

Here's a one-liner with reproducible data.


testdata <- structure(list(subject = structure(1:6, .Label = c("001-002",
"002-003", "003-004", "004-005", "005-006", "006-007"), class = "factor"),
group = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("boys",
"girls"), class = "factor"), wk1 = c(2L, 7L, 9L, 5L, 2L,
1L), wk2 = c(3L, 6L, 4L, 7L, 6L, 4L), wk3 = c(4L, 5L, 6L,
8L, 3L, 7L), wk4 = c(5L, 4L, 1L, 9L, 8L, 4L)), .Names = c("subject",
"group", "wk1", "wk2", "wk3", "wk4"), class = "data.frame", row.names = c(NA,
-6L))

testdata$subject <- as.factor(substring(as.character(testdata$subject), 1, 3))

> testdata
  subject group wk1 wk2 wk3 wk4
1 001  boys   2   3   4   5
2 002  boys   7   6   5   4
3 003  boys   9   4   6   1
4 004 girls   5   7   8   9
5 005 girls   2   6   3   8
6 006 girls   1   4   7   4
> str(testdata)
'data.frame': 6 obs. of  6 variables:
 $ subject: Factor w/ 6 levels "001","002","003",..: 1 2 3 4 5 6
 $ group  : Factor w/ 2 levels "boys","girls": 1 1 1 2 2 2
 $ wk1: int  2 7 9 5 2 1
 $ wk2: int  3 6 4 7 6 4
 $ wk3: int  4 5 6 8 3 7
 $ wk4: int  5 4 1 9 8 4

Sarah

On Fri, Mar 4, 2016 at 1:00 PM, KMNanus  wrote:
>
> Here’s the dataset I’m working with, called test -
>
> subject group wk1 wk2 wk3 wk4 place
> 001-002 boys 2 3 4 5
> 002-003 boys 7 6 5 4
> 003-004 boys 9 4 6 1
> 004-005 girls 5 7 8 9
> 005-006 girls 2 6 3 8
> 006-007 girls 1 4 7 4
>
>
> if I call mutate(test, place = substr(subject,1,3), “001 is the first 
> observation in the place column
>
> But it’s a character and “subject” is a factor.  I need place to be a factor, 
> too, but I need the observations to be ONLY the first three numbers of 
> “subject.”
>
> Does that make my request more understandable?

__
R-help@r-project.org 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.

Re: [R] help in maximum likelihood

2016-03-04 Thread Ravi Varadhan
Standard error = sqrt(diag(solve(opt$hessian)))

Ravi

From: Alaa Sindi [mailto:alaasi...@icloud.com]
Sent: Wednesday, March 02, 2016 3:22 PM
To: Ravi Varadhan 
Cc: r-help@r-project.org
Subject: Re: help in maximum likelihood

Thank you very much prof. Ravi,

That was very helpful. Is there a way to get the t and p value for the 
coefficients?

Thanks
Alaa
On Mar 2, 2016, at 10:05 AM, Ravi Varadhan 
mailto:ravi.varad...@jhu.edu>> wrote:

There is nothing wrong with the optimization.  It is a warning message.  
However, this is a good example to show that one should not simply dismiss a 
warning before understanding what it means.  The MLE parameters are also large, 
indicating that there is something funky about the model or the data or both.  
In your case, there is one major problem with the data:  for the highest dose 
(value of x), you have all subjects responding, i.e. y = n.  Even for the next 
lower dose, there is almost complete response.  Where do these data come from? 
Are they real or fake (simulated) data?

Also, take a look at the eigenvalues of the hessian at the solution.  You will 
see that there is some ill-conditioning, as the eigenvalues are widely 
separated.

x <- c(1.6907, 1.7242, 1.7552, 1.7842, 1.8113, 1.8369, 1.8610, 1.8839)
y <- c( 6, 13, 18, 28, 52, 53, 61, 60)
n <- c(59, 60, 62, 56, 63, 59, 62, 60)

# note: there is no need to have the choose(n, y) term in the likelihood
fn <- function(p)
sum( - (y*(p[1]+p[2]*x) - n*log(1+exp(p[1]+p[2]*x))) )

out <- nlm(fn, p = c(-50,20), hessian = TRUE)

out

eigen(out$hessian)


Hope this is helpful,
Ravi



Ravi Varadhan, Ph.D. (Biostatistics), Ph.D. (Environmental Engg)
Associate Professor,  Department of Oncology
Division of Biostatistics & Bionformatics
Sidney Kimmel Comprehensive Cancer Center
Johns Hopkins University
550 N. Broadway, Suite -E
Baltimore, MD 21205
410-502-2619



[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] Extracting part of a factor

2016-03-04 Thread Sarah Goslee
Hi Ken,

You do that with as.factor(), as has already been suggested. You'll need to
provide a reproducible example to show us what's going wrong. Using fake
data is fine, we just need to see some data that look like yours and the
code you're using.

Sarah

On Fri, Mar 4, 2016 at 11:57 AM, KMNanus  wrote:

> Let me see if I can ask the question more clearly - I am trying to extract
> a section of a hyphenated factor. For example, 001-004 is one observation
> of test$ken, which is a factor, and I want to set up a new factor variable
> called place that would have 001 as an observation. If I call mutate(place
> = (as.character (test$ken)), I can extract 001 from  001-004, but but don't
> know how to subsequently convert that character string back into a factor.
>
>
> Or can 001 be extracted from a factor as a factor?
>
> Do you know how to execute either of these approaches?
>
> Ken
> kmna...@gmail.com
> 914-450-0816 (tel)
> 347-730-4813 (fax)
>
>
>
> > On Mar 3, 2016, at 8:33 PM, Hervé Pagès  wrote:
> >
> > On 03/03/2016 02:13 PM, KMNanus wrote:
> >> When I do that,
> >
> > When you do what exactly?
> >
> > It's impossible for anyone here to know what you're doing if you
> > don't show the code.
> >
> >> I get "Error in `$<-.data.frame`(`*tmp*`, "site", value
> >> = integer(0)) :
> >>   replacement has 0 rows, data has 6”
> >>
> >> The data frame has 6 rows.
> >
> > You said you had a factor variable, you never mentioned you had a
> > data.frame. If the factor variable is part of a data.frame 'df',
> > then first extract it with something like df$myvar or df[["myvar"]],
> > and then call substr() followed by as.factor() on it.
> >
> > H.
> >
> >>
> >> Ken
> >> kmna...@gmail.com 
> >> 914-450-0816 (tel)
> >> 347-730-4813 (fax)
> >>
> >>
> >>> On Mar 3, 2016, at 4:52 PM, Hervé Pagès  >>> > wrote:
> >>>
> >>> Hi,
> >>>
> >>> On 03/03/2016 12:18 PM, KMNanus wrote:
>  I have a factor variable that is 6 digits and hyphenated.  For
>  example, 001-014.
> 
>  I need to extract the first 3 digits to a new variable using mutate
>  in dplyr - in this case 001 - but can’t find a function to do it.
> 
>  substr will do this for character strings, but I need the variable to
>  remain as a factor.
> >>>
> >>> What prevents you from calling as.factor() on the result to turn it
> >>> back into a factor?
> >>>
> >>> H.
> >>>
> 
>  Is there an R function  or workaround to do this?
> 
> 
>  Ken
>  kmna...@gmail.com 
>  914-450-0816 (tel)
>  347-730-4813 (fax)
> 
> 
> 
>  __
>  R-help@r-project.org 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.
> 
> >>>
> >>> --
> >>> Hervé Pagès
> >>>
> >>> Program in Computational Biology
> >>> Division of Public Health Sciences
> >>> Fred Hutchinson Cancer Research Center
> >>> 1100 Fairview Ave. N, M1-B514
> >>> P.O. Box 19024
> >>> Seattle, WA 98109-1024
> >>>
> >>> E-mail: hpa...@fredhutch.org 
> >>> Phone:  (206) 667-5791
> >>> Fax:(206) 667-1319
> >>
> >
> > --
> > Hervé Pagès
> >
> > Program in Computational Biology
> > Division of Public Health Sciences
> > Fred Hutchinson Cancer Research Center
> > 1100 Fairview Ave. N, M1-B514
> > P.O. Box 19024
> > Seattle, WA 98109-1024
> >
> > E-mail: hpa...@fredhutch.org
> > Phone:  (206) 667-5791
> > Fax:(206) 667-1319
>
>

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.

Re: [R] Extracting part of a factor

2016-03-04 Thread KMNanus
Let me see if I can ask the question more clearly - I am trying to extract a 
section of a hyphenated factor. For example, 001-004 is one observation of 
test$ken, which is a factor, and I want to set up a new factor variable called 
place that would have 001 as an observation. If I call mutate(place = 
(as.character (test$ken)), I can extract 001 from  001-004, but but don't know 
how to subsequently convert that character string back into a factor.


Or can 001 be extracted from a factor as a factor?

Do you know how to execute either of these approaches?

Ken
kmna...@gmail.com
914-450-0816 (tel)
347-730-4813 (fax)



> On Mar 3, 2016, at 8:33 PM, Hervé Pagès  wrote:
> 
> On 03/03/2016 02:13 PM, KMNanus wrote:
>> When I do that,
> 
> When you do what exactly?
> 
> It's impossible for anyone here to know what you're doing if you
> don't show the code.
> 
>> I get "Error in `$<-.data.frame`(`*tmp*`, "site", value
>> = integer(0)) :
>>   replacement has 0 rows, data has 6”
>> 
>> The data frame has 6 rows.
> 
> You said you had a factor variable, you never mentioned you had a
> data.frame. If the factor variable is part of a data.frame 'df',
> then first extract it with something like df$myvar or df[["myvar"]],
> and then call substr() followed by as.factor() on it.
> 
> H.
> 
>> 
>> Ken
>> kmna...@gmail.com 
>> 914-450-0816 (tel)
>> 347-730-4813 (fax)
>> 
>> 
>>> On Mar 3, 2016, at 4:52 PM, Hervé Pagès >> > wrote:
>>> 
>>> Hi,
>>> 
>>> On 03/03/2016 12:18 PM, KMNanus wrote:
 I have a factor variable that is 6 digits and hyphenated.  For
 example, 001-014.
 
 I need to extract the first 3 digits to a new variable using mutate
 in dplyr - in this case 001 - but can’t find a function to do it.
 
 substr will do this for character strings, but I need the variable to
 remain as a factor.
>>> 
>>> What prevents you from calling as.factor() on the result to turn it
>>> back into a factor?
>>> 
>>> H.
>>> 
 
 Is there an R function  or workaround to do this?
 
 
 Ken
 kmna...@gmail.com 
 914-450-0816 (tel)
 347-730-4813 (fax)
 
 
 
 __
 R-help@r-project.org 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.
 
>>> 
>>> --
>>> Hervé Pagès
>>> 
>>> Program in Computational Biology
>>> Division of Public Health Sciences
>>> Fred Hutchinson Cancer Research Center
>>> 1100 Fairview Ave. N, M1-B514
>>> P.O. Box 19024
>>> Seattle, WA 98109-1024
>>> 
>>> E-mail: hpa...@fredhutch.org 
>>> Phone:  (206) 667-5791
>>> Fax:(206) 667-1319
>> 
> 
> -- 
> Hervé Pagès
> 
> Program in Computational Biology
> Division of Public Health Sciences
> Fred Hutchinson Cancer Research Center
> 1100 Fairview Ave. N, M1-B514
> P.O. Box 19024
> Seattle, WA 98109-1024
> 
> E-mail: hpa...@fredhutch.org
> Phone:  (206) 667-5791
> Fax:(206) 667-1319

__
R-help@r-project.org 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.

Re: [R] difference between successive values

2016-03-04 Thread catalin roibu
I mean the first row value

În Vin, 4 mar. 2016, 16:15 Jeff Newmiller,  a
scris:

> "Keep the first values" is imprecise, but mixing an absolute value with a
> bunch of differences doesn't usually work out well.  I frequently choose
> among
>
> x <- sample( 10 )
> dxright <- c( 0, diff(x) )
> dxleft <- c( diff(x), 0 )
>
> for calculation purposes depending on my needs.
> --
> Sent from my phone. Please excuse my brevity.
>
> On March 4, 2016 3:28:08 AM PST, Olivier Crouzet <
> olivier.crou...@univ-nantes.fr> wrote:
> >Hi,
> >
> >(1) You should provide a minimal working example;
> >
> >(2) But anyway, does...
> >
> >x = sample(10)
> >c(x[1],diff(x))
> >
> >... do what you want?
> >
> >Olivier.
> >
> >
> >On Fri, 4 Mar 2016
> >13:22:07 +0200 catalin roibu  wrote:
> >
> >> Dear all!
> >>
> >> I want to calculate difference between successive values (cumulative
> >> values) with R. I used diff function, but I want to keep the first
> >> values.
> >>
> >> Please help me to solve this problem!
> >>
> >> Thank you!
> >>
> >> Best regards!
> >>
> >> CR
> >>
> >> --
> >>
> >> -
> >> -
> >> Catalin-Constantin ROIBU
> >> ​
> >> Lecturer PhD, Forestry engineer
> >> Forestry Faculty of Suceava
> >> Str. Universitatii no. 13, Suceava, 720229, Romania
> >> office phone  +4 0230 52 29 78, ext. 531
> >> mobile phone+4 0745 53 18 01
> >> FAX:+4 0230 52 16 64
> >> silvic.usv.ro 
> >>
> >>  [[alternative HTML version deleted]]
> >>
> >> __
> >> R-help@r-project.org 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.
> >
> >--
> >  Olivier Crouzet, PhD
> >  Laboratoire de Linguistique de Nantes -- UMR6310
> >  CNRS / Université de Nantes
> >  Chemin de la Censive du Tertre -- BP 81227
> >  44312 Nantes cedex 3
> >  France
> >
> >  http://www.lling.univ-nantes.fr/
> >
> >__
> >R-help@r-project.org 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]]
>
> __
> R-help@r-project.org 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.

-- 

-
-
Catalin-Constantin ROIBU
​
Lecturer PhD, Forestry engineer
​
Forestry Faculty of Suceava
Str. Universitatii no. 13, Suceava, 720229,
office phone +4 0230 52 29 78, ext. 531
mobile phone   +4 0745 53 18 01
   +4 0766 71 76 58
FAX:+4 0230 52 16 64
silvic.usv.ro 

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.

Re: [R] difference between successive values

2016-03-04 Thread Jeff Newmiller
"Keep the first values" is imprecise, but mixing an absolute value with a bunch 
of differences doesn't usually work out well.  I frequently choose among

x <- sample( 10 )
dxright <- c( 0, diff(x) )
dxleft <- c( diff(x), 0 )

for calculation purposes depending on my needs. 
-- 
Sent from my phone. Please excuse my brevity.

On March 4, 2016 3:28:08 AM PST, Olivier Crouzet 
 wrote:
>Hi,
>
>(1) You should provide a minimal working example;
>
>(2) But anyway, does...
>
>x = sample(10)
>c(x[1],diff(x))
>
>... do what you want?
>
>Olivier.
>
>
>On Fri, 4 Mar 2016
>13:22:07 +0200 catalin roibu  wrote:
>
>> Dear all!
>> 
>> I want to calculate difference between successive values (cumulative
>> values) with R. I used diff function, but I want to keep the first
>> values.
>> 
>> Please help me to solve this problem!
>> 
>> Thank you!
>> 
>> Best regards!
>> 
>> CR
>> 
>> -- 
>> 
>> -
>> -
>> Catalin-Constantin ROIBU
>> ​
>> Lecturer PhD, Forestry engineer
>> Forestry Faculty of Suceava
>> Str. Universitatii no. 13, Suceava, 720229, Romania
>> office phone  +4 0230 52 29 78, ext. 531
>> mobile phone+4 0745 53 18 01
>> FAX:+4 0230 52 16 64
>> silvic.usv.ro 
>> 
>>  [[alternative HTML version deleted]]
>> 
>> __
>> R-help@r-project.org 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.
>
>-- 
>  Olivier Crouzet, PhD
>  Laboratoire de Linguistique de Nantes -- UMR6310
>  CNRS / Université de Nantes
>  Chemin de la Censive du Tertre -- BP 81227
>  44312 Nantes cedex 3
>  France
>
>  http://www.lling.univ-nantes.fr/
>
>__
>R-help@r-project.org 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]]

__
R-help@r-project.org 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.

[R] [R-pkgs] New package: DataExplorer

2016-03-04 Thread Boxuan Cui
Dear useRs,

I am really excited that my first package is now on CRAN:
https://cran.r-project.org/web/packages/DataExplorer/.

The package aims to simplify the data exploration process before your data
analysis and/or model building process.

Example use case: One day, you get a random dataset that you have no idea
what is in there. You could simply type the following two lines of R code
to quickly visualize things and get started.

*## assume you load the data into R and name it mysterious_data*
*library(DataExplorer)*
*GenerateReport(mysterious_data)*


I am trying to add more functionalities and also vignettes to better
illustrate how to explore your data using this package. Check the GitHub
page for most up-to-date development:
https://github.com/boxuancui/DataExplorer.

I welcome all feedback, suggestions, bug reports and feature requests.
Thank you!


Best,
Boxuan (Bo)

[[alternative HTML version deleted]]

___
R-packages mailing list
r-packa...@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-packages

__
R-help@r-project.org 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.


Re: [R] difference between successive values

2016-03-04 Thread Olivier Crouzet
Hi,

(1) You should provide a minimal working example;

(2) But anyway, does...

x = sample(10)
c(x[1],diff(x))

... do what you want?

Olivier.


On Fri, 4 Mar 2016
13:22:07 +0200 catalin roibu  wrote:

> Dear all!
> 
> I want to calculate difference between successive values (cumulative
> values) with R. I used diff function, but I want to keep the first
> values.
> 
> Please help me to solve this problem!
> 
> Thank you!
> 
> Best regards!
> 
> CR
> 
> -- 
> 
> -
> -
> Catalin-Constantin ROIBU
> ​
> Lecturer PhD, Forestry engineer
> Forestry Faculty of Suceava
> Str. Universitatii no. 13, Suceava, 720229, Romania
> office phone  +4 0230 52 29 78, ext. 531
> mobile phone+4 0745 53 18 01
> FAX:+4 0230 52 16 64
> silvic.usv.ro 
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@r-project.org 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.

-- 
  Olivier Crouzet, PhD
  Laboratoire de Linguistique de Nantes -- UMR6310
  CNRS / Université de Nantes
  Chemin de la Censive du Tertre -- BP 81227
  44312 Nantes cedex 3
  France

  http://www.lling.univ-nantes.fr/

__
R-help@r-project.org 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.

[R] difference between successive values

2016-03-04 Thread catalin roibu
Dear all!

I want to calculate difference between successive values (cumulative
values) with R. I used diff function, but I want to keep the first values.

Please help me to solve this problem!

Thank you!

Best regards!

CR

-- 

-
-
Catalin-Constantin ROIBU
​
Lecturer PhD, Forestry engineer
Forestry Faculty of Suceava
Str. Universitatii no. 13, Suceava, 720229, Romania
office phone  +4 0230 52 29 78, ext. 531
mobile phone+4 0745 53 18 01
FAX:+4 0230 52 16 64
silvic.usv.ro 

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.

Re: [R] ALLOCATE in a FORTRAN subroutine

2016-03-04 Thread Berend Hasselman

> On 4 Mar 2016, at 08:51, MAURICE Jean - externe  
> wrote:
> 
> Hi Berend,
> 
> 
>> The question belongs on the R-devel mailinglist.
> I try to find this mailing-list ...
> 

See https://www.r-project.org/mail.html

>> You are calling your Fortran routines directly from an R file with .Fortran, 
>> I presume?
> Yes. Is there another solution (possibility ?)
> 

Yes. Use C to interface with Fortran.

See for example packages: minpack.lm, QZ, expm, nleqslv. And more: rootSolve, 
ucminf.
There are probably even more.

>> Declare them with (:,:) or (*,*), allocate and the return the array? A 
>> Fortran95 pointer?
> That was what I wanted to do ...
> 
>> I think  all of that is just a no no.
> It would be a big problem with memory management but R could copy the result 
> before the memory attached to the FORTRAN subroutine is cleared at the end of 
> the routine. R does so strange things (for an old programmer as I am).
> 
>> Is it possible to calculate the required size of the array in a separate 
>> routine given some parameters (as can often be done with >Lapack routines) 
>> before calling the main subroutine?
>> If so, call the sizing routine and then declare the correctly sized array in 
>> your R code and pass it to the main subroutine.
> 
> It's a good idea : a 'two shots' routine. I'll dig that (I'll work in this 
> direction).
> 

Commercial:  Have a look at (my) packages nleqslv and geigen for that approach.

Berend

__
R-help@r-project.org 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.