Re: [R] Retaining attributes of columns of a data frame when subsetting.

2019-10-19 Thread Rolf Turner

On 20/10/19 3:00 PM, Richard M. Heiberger wrote:

Look at
methods(as.data.frame)
Define your specialized columns to have a newly defined class, say "myclass".
Then write as.data.frame.myclass
It will be similar to the function you already have in the lapply statement.
Now your statement
X <- X[ok,]
should work.


Yes.  That idea does indeed look promising.  I'll check it out.
Thanks.

cheers,

Rolf

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

__
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] Retaining attributes of columns of a data frame when subsetting.

2019-10-19 Thread Richard M. Heiberger
Look at
methods(as.data.frame)
Define your specialized columns to have a newly defined class, say "myclass".
Then write as.data.frame.myclass
It will be similar to the function you already have in the lapply statement.
Now your statement
X <- X[ok,]
should work.

Rich

On Sat, Oct 19, 2019 at 8:20 PM Rolf Turner  wrote:
>
>
> I am writing a function that involves a data frame "X" some columns of
> which have attributes.  I replace X by a data frame with a subset of the
> rows of X:
>
>  X <- X[ok,]
>
> where "ok" is a logical vector.  When I do this the attributes of the
> columns (which I need to retain) are lost (except for the "class" and
> "levels" attributes of columns which are factors).
>
> Is there any sexy way to retain the attributes of the columns?
>
> So far the only approach that I can work out is to extract the
> attributes prior to subsetting and put them back after subsetting.
>
> Like unto:
>
>  SaveAt <- lapply(X,attributes)
>  X <- X[ok,]
>  lX <- lapply(names(X),function(nm,x,Sat){
> attributes(x[[nm]]) <- Sat[[nm]]
> x[[nm]]},x=X,Sat=SaveAt)
>  names(lX) <- names(X)
>  X <- as.data.frame(lX)
>
> This seems to work, but is rather kludgy.  Is there a better way?
>
> Thanks for any pointers.
>
> cheers,
>
> Rolf Turner
>
> --
> Honorary Research Fellow
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
> __
> 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.


[R] Retaining attributes of columns of a data frame when subsetting.

2019-10-19 Thread Rolf Turner



I am writing a function that involves a data frame "X" some columns of 
which have attributes.  I replace X by a data frame with a subset of the 
rows of X:


X <- X[ok,]

where "ok" is a logical vector.  When I do this the attributes of the 
columns (which I need to retain) are lost (except for the "class" and 
"levels" attributes of columns which are factors).


Is there any sexy way to retain the attributes of the columns?

So far the only approach that I can work out is to extract the 
attributes prior to subsetting and put them back after subsetting.


Like unto:

SaveAt <- lapply(X,attributes)
X <- X[ok,]
lX <- lapply(names(X),function(nm,x,Sat){
   attributes(x[[nm]]) <- Sat[[nm]]
   x[[nm]]},x=X,Sat=SaveAt)
names(lX) <- names(X)
X <- as.data.frame(lX)

This seems to work, but is rather kludgy.  Is there a better way?

Thanks for any pointers.

cheers,

Rolf Turner

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

__
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] Preserving numeric columns

2019-10-19 Thread Felipe Carrillo via R-help
Yes, options(knitr.kable.NA = '-')  is The answer for kable.
Do you happen to know what are the arguments used for gridExtra grid.draw to 
acomplish the same thing?

Sent from Yahoo Mail on Android 
 
  On Sat, Oct 19, 2019 at 1:01 PM, Jeff Newmiller 
wrote:   Then the polite next step is for you to indicate what that solution 
was so people searching the archives can learn from your question. Was it to 
set the kable option?

options(knitr.kable.NA = '-') 

On October 19, 2019 12:50:20 PM PDT, Felipe Carrillo  
wrote:
>You are correct. I didnt explain well and failed to mention that this
>is for knitr::kable. I already figured it out. 
>
>Sent from Yahoo Mail on Android 
> 
>On Sat, Oct 19, 2019 at 1:04 AM, Jeff
>Newmiller wrote:  Data frames are NOT
>spreadsheets. Don't treat them like spreadsheets. All elements in a
>column are parts of a vector which means they all have the same data
>type.
>
>On the other hand, if you want to generate formatted output in HTML,
>LaTeX, or Word, there are many tools for generating formatted tables in
>the data output phase of data analysis, and it is common to convert
>everything to character format intentionally then.
>
>On October 19, 2019 12:44:26 AM PDT, Felipe Carrillo via R-help
> wrote:
>>Consider the following dataset:  I need to replace NAs with "-" but I
>>lose my numeric formatting  fall.estimate <- structure(list(`Salmon` =
>>c("salmon River", "Ant Creek", "big Creek", "oso River", "linda
>>Creek"), `baseline` = c(80874.384012, 361.1997, 5012.8311, 638.6912,
>>402.1044), `target` = c(16, 720, 1, 450, 800), `1992`
>=
>>c(27618.4365, 0, 3587.61719, NA, NA), `1993` = c(100027.82328, NA,
>>5647.83116, NA, NA), `1994` = c(99414.57438, NA, 12896.93753, NA, NA),
>>`1995` = c(235027.00518, NA, 32059.63037, NA, NA), `1996` =
>>c(143004.6423, NA, 17191.2152, NA, NA), `1997` = c(112796.88894, NA,
>>27365.24435, NA, NA), `1998` = c(102858.8148, NA, 20539.17372, NA,
>NA),
>>`1999` = c(94113.26562, NA, 21916.44213, NA, NA)), row.names =
>>c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"))
>>fall.estimatestr(fall.estimate)#convert to class
>dataframefall.estimate
>><- as.data.frame(fall.estimate)
>>#Remove all decimalsfall.estimate[,-1]
>><-round(fall.estimate[,-1],0)#Replace NA's' with dash
>>'-'fall.estimate[is.na(fall.estimate)] <- "-"
>>#Here all my columns get converted to character#Try to convert back to
>>numericfall.estimate <- mutate_all(fall.estimate, function(x)
>>as.numeric(as.character(x))) fall.estimate#But I get these warnings
>>aand my dashes dissapearQuestion: How can I replace my NAs with dashes
>>and keep all my dataframecolumns as numeric? Warning messages:1: In
>>FUN(newX[, i], ...) : NAs introduced by coercion2: In FUN(newX[, i],
>>...) : NAs introduced by coercion3: In FUN(newX[, i], ...) : NAs
>>introduced by coercion4: In FUN(newX[, i], ...) : NAs introduced by
>>coercion5: In FUN(newX[, i], ...) : NAs introduced by coercion6: In
>>FUN(newX[, i], ...) : NAs introduced by coercion7: In FUN(newX[, i],
>>...) : NAs introduced by coercion8: In FUN(newX[, i], ...) : NAs
>>introduced by coercion9: In FUN(newX[, i], ...) : NAs introduced by
>>coercion
>>Thanks beforehand
>>
>>
>>    [[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.

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

[[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] L1 (lasso) regularized log-linear model selection procedure

2019-10-19 Thread Bert Gunter
Searching on "lasso penalty with deviance" on rseek.org  brought up many
packages.

-- Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sat, Oct 19, 2019 at 7:54 AM Davor Josipovic  wrote:

> Daphne Koller (2009) describes L1 regularization (Chapter 20) as an
> efficient way for Markov network (i.e. undirected graphical model)
> structure learning and feature parameter estimation.
>
> Her focus, and mine, are log-linear models for high-dimensional
> contingency tables (i.e. categorical data).
>
> I wonder whether there are any good implementations of this?
>
> I have looked here (https://cran.r-project.org/web/views/gR.html) and
> found only implementations for continuous data:
> * parcor: Regularized estimation of partial correlation matrices
> * glasso: Graphical Lasso: Estimation of Gaussian Graphical Models
>
> Both are for continuous (Gaussian) data, not categorical.
>
> Any suggestions?
> __
> 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] Preserving numeric columns

2019-10-19 Thread Jeff Newmiller
Then the polite next step is for you to indicate what that solution was so 
people searching the archives can learn from your question. Was it to set the 
kable option?

options(knitr.kable.NA = '-') 

On October 19, 2019 12:50:20 PM PDT, Felipe Carrillo  
wrote:
>You are correct. I didnt explain well and failed to mention that this
>is for knitr::kable. I already figured it out. 
>
>Sent from Yahoo Mail on Android 
> 
>On Sat, Oct 19, 2019 at 1:04 AM, Jeff
>Newmiller wrote:   Data frames are NOT
>spreadsheets. Don't treat them like spreadsheets. All elements in a
>column are parts of a vector which means they all have the same data
>type.
>
>On the other hand, if you want to generate formatted output in HTML,
>LaTeX, or Word, there are many tools for generating formatted tables in
>the data output phase of data analysis, and it is common to convert
>everything to character format intentionally then.
>
>On October 19, 2019 12:44:26 AM PDT, Felipe Carrillo via R-help
> wrote:
>>Consider the following dataset:  I need to replace NAs with "-" but I
>>lose my numeric formatting  fall.estimate <- structure(list(`Salmon` =
>>c("salmon River", "Ant Creek", "big Creek", "oso River", "linda
>>Creek"), `baseline` = c(80874.384012, 361.1997, 5012.8311, 638.6912,
>>402.1044), `target` = c(16, 720, 1, 450, 800), `1992`
>=
>>c(27618.4365, 0, 3587.61719, NA, NA), `1993` = c(100027.82328, NA,
>>5647.83116, NA, NA), `1994` = c(99414.57438, NA, 12896.93753, NA, NA),
>>`1995` = c(235027.00518, NA, 32059.63037, NA, NA), `1996` =
>>c(143004.6423, NA, 17191.2152, NA, NA), `1997` = c(112796.88894, NA,
>>27365.24435, NA, NA), `1998` = c(102858.8148, NA, 20539.17372, NA,
>NA),
>>`1999` = c(94113.26562, NA, 21916.44213, NA, NA)), row.names =
>>c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"))
>>fall.estimatestr(fall.estimate)#convert to class
>dataframefall.estimate
>><- as.data.frame(fall.estimate)
>>#Remove all decimalsfall.estimate[,-1]
>><-round(fall.estimate[,-1],0)#Replace NA's' with dash
>>'-'fall.estimate[is.na(fall.estimate)] <- "-"
>>#Here all my columns get converted to character#Try to convert back to
>>numericfall.estimate <- mutate_all(fall.estimate, function(x)
>>as.numeric(as.character(x))) fall.estimate#But I get these warnings
>>aand my dashes dissapearQuestion: How can I replace my NAs with dashes
>>and keep all my dataframecolumns as numeric? Warning messages:1: In
>>FUN(newX[, i], ...) : NAs introduced by coercion2: In FUN(newX[, i],
>>...) : NAs introduced by coercion3: In FUN(newX[, i], ...) : NAs
>>introduced by coercion4: In FUN(newX[, i], ...) : NAs introduced by
>>coercion5: In FUN(newX[, i], ...) : NAs introduced by coercion6: In
>>FUN(newX[, i], ...) : NAs introduced by coercion7: In FUN(newX[, i],
>>...) : NAs introduced by coercion8: In FUN(newX[, i], ...) : NAs
>>introduced by coercion9: In FUN(newX[, i], ...) : NAs introduced by
>>coercion
>>Thanks beforehand
>>
>>
>>    [[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.

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

__
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] Preserving numeric columns

2019-10-19 Thread Felipe Carrillo via R-help
You are correct. I didnt explain well and failed to mention that this is for 
knitr::kable. I already figured it out. 

Sent from Yahoo Mail on Android 
 
  On Sat, Oct 19, 2019 at 1:04 AM, Jeff Newmiller 
wrote:   Data frames are NOT spreadsheets. Don't treat them like spreadsheets. 
All elements in a column are parts of a vector which means they all have the 
same data type.

On the other hand, if you want to generate formatted output in HTML, LaTeX, or 
Word, there are many tools for generating formatted tables in the data output 
phase of data analysis, and it is common to convert everything to character 
format intentionally then.

On October 19, 2019 12:44:26 AM PDT, Felipe Carrillo via R-help 
 wrote:
>Consider the following dataset:  I need to replace NAs with "-" but I
>lose my numeric formatting  fall.estimate <- structure(list(`Salmon` =
>c("salmon River", "Ant Creek", "big Creek", "oso River", "linda
>Creek"), `baseline` = c(80874.384012, 361.1997, 5012.8311, 638.6912,
>402.1044), `target` = c(16, 720, 1, 450, 800), `1992` =
>c(27618.4365, 0, 3587.61719, NA, NA), `1993` = c(100027.82328, NA,
>5647.83116, NA, NA), `1994` = c(99414.57438, NA, 12896.93753, NA, NA),
>`1995` = c(235027.00518, NA, 32059.63037, NA, NA), `1996` =
>c(143004.6423, NA, 17191.2152, NA, NA), `1997` = c(112796.88894, NA,
>27365.24435, NA, NA), `1998` = c(102858.8148, NA, 20539.17372, NA, NA),
>`1999` = c(94113.26562, NA, 21916.44213, NA, NA)), row.names =
>c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"))
>fall.estimatestr(fall.estimate)#convert to class dataframefall.estimate
><- as.data.frame(fall.estimate)
>#Remove all decimalsfall.estimate[,-1]
><-round(fall.estimate[,-1],0)#Replace NA's' with dash
>'-'fall.estimate[is.na(fall.estimate)] <- "-"
>#Here all my columns get converted to character#Try to convert back to
>numericfall.estimate <- mutate_all(fall.estimate, function(x)
>as.numeric(as.character(x))) fall.estimate#But I get these warnings
>aand my dashes dissapearQuestion: How can I replace my NAs with dashes
>and keep all my dataframecolumns as numeric? Warning messages:1: In
>FUN(newX[, i], ...) : NAs introduced by coercion2: In FUN(newX[, i],
>...) : NAs introduced by coercion3: In FUN(newX[, i], ...) : NAs
>introduced by coercion4: In FUN(newX[, i], ...) : NAs introduced by
>coercion5: In FUN(newX[, i], ...) : NAs introduced by coercion6: In
>FUN(newX[, i], ...) : NAs introduced by coercion7: In FUN(newX[, i],
>...) : NAs introduced by coercion8: In FUN(newX[, i], ...) : NAs
>introduced by coercion9: In FUN(newX[, i], ...) : NAs introduced by
>coercion
>Thanks beforehand
>
>
>    [[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.

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

[[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] "chi-square" | "chi-squared" | "chi squared" | "chi, square"

2019-10-19 Thread William Dunlap via R-help
Sigma squared or sigma square?  Hotelling's T-squared or T-square?

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Sat, Oct 19, 2019 at 7:38 AM Therneau, Terry M., Ph.D. via R-help <
r-help@r-project.org> wrote:

> Martin,
>A fun question.
>
> Looking back at my oldest books, Feller (1950) used chi-square.
> Then I walked down the hall to our little statistics library and looked at
> Johnson and
> Kotz, "Continous Univariate Distributions", since each chapter therein has
> comments about
> the history of the distribution.
>
>   a.  They use 'chi-square' throughout their history section, tracing the
> distribution
> back to work in the 1800s.  But, those earliest papers apparently didn't
> name their
> results as chi- whatever, so an "origin" story didn't pan out.
>
>   b. They have 13 pages of references, and for fun I counted the occurence
> of variants.
> The majority of papers don't have the word in the title at all and the
> next most common is
> the Greek symbol. Here are the years of the others:
>
> chi-square:   73 43 65 80 86 73 82 73 69 69 78 64 64 86 65 86 82 82 76 82
> 88 81 74 77 87
> 86 93 69 60 88 88 80 77 41 59 79 31
> chi-squared: 72 76 82 83 89 79 69 67 77 78 69 77 83 88 87 89 78
> chi:  92 73 89 87
> chi-squares: 77 83
> chi-bar-square: 91
>
> There doesn't look to be a trend over time.  The 1922 Fisher reference
> uses the Greek
> symbol, by the way.
>
> Terry T
>
>
> [[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.
>

[[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] L1 (lasso) regularized log-linear model selection procedure

2019-10-19 Thread Davor Josipovic
Daphne Koller (2009) describes L1 regularization (Chapter 20) as an
efficient way for Markov network (i.e. undirected graphical model)
structure learning and feature parameter estimation.

Her focus, and mine, are log-linear models for high-dimensional
contingency tables (i.e. categorical data).

I wonder whether there are any good implementations of this?

I have looked here (https://cran.r-project.org/web/views/gR.html) and
found only implementations for continuous data:
* parcor: Regularized estimation of partial correlation matrices
* glasso: Graphical Lasso: Estimation of Gaussian Graphical Models

Both are for continuous (Gaussian) data, not categorical.

Any suggestions?
__
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] "chi-square" | "chi-squared" | "chi squared" | "chi, square"

2019-10-19 Thread Therneau, Terry M., Ph.D. via R-help
Martin,
   A fun question.

Looking back at my oldest books, Feller (1950) used chi-square.
Then I walked down the hall to our little statistics library and looked at 
Johnson and 
Kotz, "Continous Univariate Distributions", since each chapter therein has 
comments about 
the history of the distribution.

  a.  They use 'chi-square' throughout their history section, tracing the 
distribution 
back to work in the 1800s.  But, those earliest papers apparently didn't name 
their 
results as chi- whatever, so an "origin" story didn't pan out.

  b. They have 13 pages of references, and for fun I counted the occurence of 
variants.  
The majority of papers don't have the word in the title at all and the next 
most common is 
the Greek symbol. Here are the years of the others:

chi-square:   73 43 65 80 86 73 82 73 69 69 78 64 64 86 65 86 82 82 76 82 88 81 
74 77 87 
86 93 69 60 88 88 80 77 41 59 79 31
chi-squared: 72 76 82 83 89 79 69 67 77 78 69 77 83 88 87 89 78
chi:  92 73 89 87
chi-squares: 77 83
chi-bar-square: 91

There doesn't look to be a trend over time.  The 1922 Fisher reference uses the 
Greek 
symbol, by the way.

Terry T


[[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] Calculating returns of bid using R

2019-10-19 Thread Benoit Vaillant
Hello,

See inline.

On Sat, Oct 19, 2019 at 01:48:44PM +0530, Sri Priya wrote:
> Dear R Users,
> Question:
> 
> A chit fund has 25 members. Each month they contribute Rs 2000 each. End of
> the month, the person who bids the lowest for the corpus, gets his bid. The
> group organizer gets paid a fixed commission of Rs 2500 each month (5% of
> 25*2000).It is deducted from the bid winner's corpus. The remaining amount
> is distributed among all the 25 customers equally.
> Based on the above details calculate using R
> 
> 1. What is the Annualized  Return of the person who bids in the last month
> ?
> 2. What is the Annualized Return of the person who bids in the first month ?
> 3. Write an R script which calculates the annualized return of chit fund
> participant ?- Show the Return % for each month's bid winner.

This list has a no homework policy.

Please provide at least some preliminary works.

> Herewith attached the excel file for calculation.

File got stripped by the list.

See more on the posting guide which is included in the footer of every
mail.

-- 
Benoît


signature.asc
Description: PGP signature
__
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] List of data frame

2019-10-19 Thread Rui Barradas

Hello,

Why not use read.xlsx argument 'na.strings', an argument that exists in 
many file reading functions? (read.table, and derivatives.)



test <- lapply(sheets,function(i) {
  read.xlsx("rainfall.xlsx", sheet = i,
startRow = 8, cols = 1:2,
na.strings = "")
})


Hope this helps,

Rui Barradas

Às 02:38 de 18/10/19, ani jaya escreveu:

Dear R-Help,

I have a list of data frame that I import from excel file using read.xlsx
command.

sheets <- openxlsx::getSheetNames("rainfall.xlsx")
test <- lapply(sheets,function(i) read.xlsx("rainfall.xlsx", sheet=i,
startRow=8, cols=1:2))
names(test) <- sprintf("%i", 1986:2015)

And I got a data.frame with 365 rows and two columns contains date and
rainfall data.
There is a value in rainfall data that i want to change as NA ()

test[1]$`1986`
Date   RR
1   01-01-19860
2   02-01-19867
3   03-01-1986   72
4   04-01-19864
5   05-01-1986   19
6   06-01-19864
7   07-01-1986   16
8   08-01-1986   21
9   09-01-1986   34
10  10-01-1986   72
11  11-01-1986   93
12  12-01-1986  178
13  13-01-1986   86
14  14-01-1986   11
15  15-01-19860
16  16-01-1986   31
17  17-01-1986   22
18  18-01-1986   18
19  19-01-19863
20  20-01-19860
21  21-01-1986   31
22  22-01-1986   46
23  23-01-19864
24  24-01-1986   40
25  25-01-1986   63
26  26-01-1986  125
27  27-01-1986   33
28  28-01-1986   44
29  29-01-19866
30  30-01-19860
31  31-01-19860
32  01-02-19862
33  02-02-1986   71
34  03-02-1986 
35  04-02-19860
36  05-02-19860
37  06-02-1986   56
38  07-02-1986   19
39  08-02-19863
40  09-02-19867
41  10-02-1986   24
42  11-02-1986   55
43  12-02-19860
44  13-02-19860

[[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-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] Calculating returns of bid using R

2019-10-19 Thread Sri Priya
Dear R Users,

I am practicing problems in the field of finance and economics. While
searching, I got problem like this. And I want to solve the following
problem using R. I dont have any clue how to incorporate this in R. Please
help me!!

Question:

A chit fund has 25 members. Each month they contribute Rs 2000 each. End of
the month, the person who bids the lowest for the corpus, gets his bid. The
group organizer gets paid a fixed commission of Rs 2500 each month (5% of
25*2000).It is deducted from the bid winner's corpus. The remaining amount
is distributed among all the 25 customers equally.
Based on the above details calculate using R

1. What is the Annualized  Return of the person who bids in the last month
?
2. What is the Annualized Return of the person who bids in the first month ?
3. Write an R script which calculates the annualized return of chit fund
participant ?- Show the Return % for each month's bid winner.

Herewith attached the excel file for calculation.

Thanks
Sripriya
__
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] Preserving numeric columns

2019-10-19 Thread Jeff Newmiller
Data frames are NOT spreadsheets. Don't treat them like spreadsheets. All 
elements in a column are parts of a vector which means they all have the same 
data type.

On the other hand, if you want to generate formatted output in HTML, LaTeX, or 
Word, there are many tools for generating formatted tables in the data output 
phase of data analysis, and it is common to convert everything to character 
format intentionally then.

On October 19, 2019 12:44:26 AM PDT, Felipe Carrillo via R-help 
 wrote:
>Consider the following dataset:  I need to replace NAs with "-" but I
>lose my numeric formatting  fall.estimate <- structure(list(`Salmon` =
>c("salmon River", "Ant Creek", "big Creek", "oso River", "linda
>Creek"), `baseline` = c(80874.384012, 361.1997, 5012.8311, 638.6912,
>402.1044), `target` = c(16, 720, 1, 450, 800), `1992` =
>c(27618.4365, 0, 3587.61719, NA, NA), `1993` = c(100027.82328, NA,
>5647.83116, NA, NA), `1994` = c(99414.57438, NA, 12896.93753, NA, NA),
>`1995` = c(235027.00518, NA, 32059.63037, NA, NA), `1996` =
>c(143004.6423, NA, 17191.2152, NA, NA), `1997` = c(112796.88894, NA,
>27365.24435, NA, NA), `1998` = c(102858.8148, NA, 20539.17372, NA, NA),
>`1999` = c(94113.26562, NA, 21916.44213, NA, NA)), row.names =
>c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"))
>fall.estimatestr(fall.estimate)#convert to class dataframefall.estimate
><- as.data.frame(fall.estimate)
>#Remove all decimalsfall.estimate[,-1]
><-round(fall.estimate[,-1],0)#Replace NA's' with dash
>'-'fall.estimate[is.na(fall.estimate)] <- "-"
>#Here all my columns get converted to character#Try to convert back to
>numericfall.estimate <- mutate_all(fall.estimate, function(x)
>as.numeric(as.character(x))) fall.estimate#But I get these warnings
>aand my dashes dissapearQuestion: How can I replace my NAs with dashes
>and keep all my dataframecolumns as numeric? Warning messages:1: In
>FUN(newX[, i], ...) : NAs introduced by coercion2: In FUN(newX[, i],
>...) : NAs introduced by coercion3: In FUN(newX[, i], ...) : NAs
>introduced by coercion4: In FUN(newX[, i], ...) : NAs introduced by
>coercion5: In FUN(newX[, i], ...) : NAs introduced by coercion6: In
>FUN(newX[, i], ...) : NAs introduced by coercion7: In FUN(newX[, i],
>...) : NAs introduced by coercion8: In FUN(newX[, i], ...) : NAs
>introduced by coercion9: In FUN(newX[, i], ...) : NAs introduced by
>coercion
>Thanks beforehand
>
>
>   [[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.

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

__
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] Preserving numeric columns

2019-10-19 Thread Felipe Carrillo via R-help
Consider the following dataset:  I need to replace NAs with "-" but I lose my 
numeric formatting  fall.estimate <- structure(list(`Salmon` = c("salmon 
River", "Ant Creek", "big Creek", "oso River", "linda Creek"), `baseline` = 
c(80874.384012, 361.1997, 5012.8311, 638.6912, 402.1044), `target` = 
c(16, 720, 1, 450, 800), `1992` = c(27618.4365, 0, 3587.61719, NA, NA), 
`1993` = c(100027.82328, NA, 5647.83116, NA, NA), `1994` = c(99414.57438, NA, 
12896.93753, NA, NA), `1995` = c(235027.00518, NA, 32059.63037, NA, NA), `1996` 
= c(143004.6423, NA, 17191.2152, NA, NA), `1997` = c(112796.88894, NA, 
27365.24435, NA, NA), `1998` = c(102858.8148, NA, 20539.17372, NA, NA), `1999` 
= c(94113.26562, NA, 21916.44213, NA, NA)), row.names = c(NA, -5L), class = 
c("tbl_df", "tbl", "data.frame"))
fall.estimatestr(fall.estimate)#convert to class dataframefall.estimate <- 
as.data.frame(fall.estimate)
#Remove all decimalsfall.estimate[,-1] <-round(fall.estimate[,-1],0)#Replace 
NA's' with dash '-'fall.estimate[is.na(fall.estimate)] <- "-"
#Here all my columns get converted to character#Try to convert back to 
numericfall.estimate <- mutate_all(fall.estimate, function(x) 
as.numeric(as.character(x))) fall.estimate#But I get these warnings aand my 
dashes dissapearQuestion: How can I replace my NAs with dashes and keep all my 
dataframecolumns as numeric? Warning messages:1: In FUN(newX[, i], ...) : NAs 
introduced by coercion2: In FUN(newX[, i], ...) : NAs introduced by coercion3: 
In FUN(newX[, i], ...) : NAs introduced by coercion4: In FUN(newX[, i], ...) : 
NAs introduced by coercion5: In FUN(newX[, i], ...) : NAs introduced by 
coercion6: In FUN(newX[, i], ...) : NAs introduced by coercion7: In FUN(newX[, 
i], ...) : NAs introduced by coercion8: In FUN(newX[, i], ...) : NAs introduced 
by coercion9: In FUN(newX[, i], ...) : NAs introduced by coercion
Thanks beforehand


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