[R] [R-pkgs] New package - LGDtoolkit
Dear all, my new R package LGDtoolkit is now on CRAN. The goal of this package is to cover the most common steps in Loss Given Default (LGD) rating model development. The main procedures available are those that refer to bivariate and multivariate analysis. In particular two statistical methods for multivariate analysis are currently implemented – OLS regression and fractional logistic regression. Both methods are also available within different blockwise model designs, and both have customized stepwise algorithms. To cover other important steps for LGD model development, it is recommended to use 'LGDtoolkit' package along with 'PDtoolkit', and 'monobin' (or 'monobinShiny') packages. With the next releases package will be extended with a new features. In particular, validation procedures will be added, and significant part of the package will be dedicated to the usage of survival analysis for LGD modeling (extrapolation of LGD for incomplete recoveries, development of risk differentiation function and collateral haircut modeling). To check out the current package functionalities run install.packages("LGDtoolkit") and help(package = "LGDtoolkit"). To stay up to date with new development features follow the github page: https://github.com/andrija-djurovic/LGDtoolkit. I hope some of you will find the package useful. Kind regards, Andrija [[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.
[R] [R-pkgs] PDtoolkit: Collection of Tools for PD Rating Model Development
Dear R users, I am happy to announce that my new package PDtoolkit is now available on CRAN. The goal of this package is to cover the most common steps in probability of default (PD) rating model development and validation. The main procedures available are those that refer to univariate, bivariate, multivariate analysis, calibration and validation. Along with accompanied monobin and monobinShiny packages, PDtoolkit provides functions which are suitable for different data transformation and modeling tasks such as: imputations, monotonic binning of numeric risk factors, binning of categorical risk factors, weights of evidence (WoE) and information value (IV) calculations, WoE coding (replacement of risk factors modalities with WoE values), risk factor clustering, area under curve (AUC) calculation and others. Additionally, package provides set of validation functions for testing homogeneity, heterogeneity, discriminatory and predictive power of the model. For details and examples please check out the github page https://github.com/andrija-djurovic/PDtoolkit or README file on CRAN page https://CRAN.R-project.org/package=PDtoolkit Best regards, Andrija Djurovic [[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.
[R] [R-pkgs] monobin: new version 0.2.0
Dear R users, the new version of monobin package is now on CRAN. Additional binning algorithm is implemented - monotonic binning driven by decision tree (mdt.bin). For details and examples, install the new version of the monobin (install.packages("monobin")) and check the help page of the function (?mdt.bin) or visit the github page: https://github.com/andrija-djurovic/monobin Soon I will upload to the CRAN updated version of monobinShiny package in order to reflect changes from the monobin. Until then users can install github version (https://github.com/andrija-djurovic/monobinShiny) where new binning algorithm is already implemented. BR, Andrija Djurovic [[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.
[R] [R-pkgs] monobinShiny: Shiny User Interface for 'monobin' Package
Dear R users, I am happy to announce that my new package monobinShiny is now available on CRAN. This is an add-on package to the 'monobin' package that simplifies its use. As reminder, the goal of 'monobin' is to perform monotonic binning of numeric risk factor in credit rating models (PD, LGD, EAD) development. All functions handle both binary and continuous target variable. Missing values and other possible special values are treated separately from so-called complete cases. 'monobinShiny' provides shiny-based user interface to 'monobin' package and it can be especially handy for less experienced R users as well as for those who intend to perform quick scanning of numeric risk factors when building credit rating models. The additional functions implemented in 'monobinShiny' that do no exist in 'monobin' package are: descriptive statistics, special case and outliers imputation. The function descriptive statistics is exported and can be used in R sessions independently from the user interface, while the special case and the outlier imputation functions are written to be used with shiny UI. Here is, also, the link to github page: https://github.com/andrija-djurovic/monobinShiny Hope that some of you will find the package useful. Best regards, Andrija Djurovic [[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.
[R] [R-pkgs] monobin: Monotonic Binning for Credit Rating Models
Dear R users, I am happy to announce that my package monobin is now available on CRAN. The package contains functions that perform monotonic binning of numeric risk factor in credit rating models (PD, LGD, EAD) development. All functions handle both binary and continuous target variable. Functions that use isotonic regression in the first stage of binning process have an additional feature for correction of minimum percentage of observations and minimum target rate per bin. Additionally, monotonic trend can be identified based on raw data or, if known in advance, forced by functions' argument. Missing values and other possible special values are treated separately from so-called complete cases. Close attention should be paid when modeling continuous target due to possible negative values. Hope that some of you will find the package useful. Best regards, Andrija Djurovic [[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] Zoo rolling window with increasing window size
Something like this? set.seed(123) y <- rnorm(20) sapply(1:length(y), function(x) sum(y[1:x])) or this, depending what is the output of your custom function lapply(1:length(y), function(x) sum(y[1:x])) On Thu, Aug 10, 2017 at 8:39 PM, Christofer Bogaso < bogaso.christo...@gmail.com> wrote: > Hi Joshua, thanks for your prompt reply. However as I said, sum() > function I used here just for demonstrating the problem, I have other > custom function to implement, not necessarily sum() > > I am looking for a generic solution for above problem. > > Any better idea? Thanks, > > On Fri, Aug 11, 2017 at 12:04 AM, Joshua Ulrich> wrote: > > Use a `width` of integer index locations. And you likely want = > > "right" (or rollapplyr(), as I used). > > > > R> set.seed(21) > > R> x <- rnorm(10) > > R> rs <- rollapplyr(x, seq_along(x), sum) > > R> cs <- cumsum(x) > > R> identical(rs, cs) > > [1] TRUE > > > > > > On Thu, Aug 10, 2017 at 1:28 PM, Christofer Bogaso > > wrote: > >> Hi again, > >> > >> I am wondering there is any function for 'zoo' time series, where I > >> can apply a user defined function rolling window basis, wherein window > >> size is ever increasing i.e. not fixed. For example, let say I have > >> below user defined function and a zoo time series : > >> > >>> library(zoo) > >> > >>> UDF = function(x) sum(x) > >> > >>> TS = zoo(rnorm(10), seq(as.Date('2017-01-01'), as.Date('2017-01-10'), > by = '1 day')) > >> > >>> > >> > >> Now I want to apply UDF (this can be any custom function, however here > >> I put it just quick example) rolling window basis like : > >> > >> 1st data point = 1st data point of TS > >> 2nd data point = sum of 1st and 2nd data points of TS > >> 3rd data point = sum of 1st 2nd and 3rd data points of TS > >> > >> so on > >> > >> I am aware of the rollapply() function from zoo, however, appears like > >> it is only for fixed window size. > >> > >> Appreciate any pointer how to achieve above strategy of implementing > >> rolling calculation based on increased window size. > >> > >> Thanks for your time. > >> > >> __ > >> 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. > > > > > > > > -- > > Joshua Ulrich | about.me/joshuaulrich > > FOSS Trading | www.fosstrading.com > > R/Finance 2017 | www.rinfinance.com > > __ > 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] Creating a new column with the number of the observation
Hi. Here is one approach: x - rep(c(A, B, C), c(3,1,2)) DF - data.frame(x, stringsAsFactors=FALSE) cbind(DF, new_c=c(lapply(rle(DF$x)[[1]], function(x) 1:x), recursive=T)) Andrija On Wed, May 28, 2014 at 10:46 PM, Marcos Santos mmsantos...@gmail.comwrote: I have a data frame like this: Category Observed_value A 100 A 130 A 140 B 90 C 80 D 120 D 130 I need to create an index column that show the number of the observation for each category. I have three observations in the category A, one in category B, one in category C and two in category D. The new matrix should be like this: Category Observed_value Index A 100 1 A 130 2 A 140 3 B 90 1 C 80 1 D 120 1 D 130 2 Please, could anyone help me with this problem? Thank you. -- Marcos Martins Santos mmsantos...@gmail.com -- Skype: marcosmartinssantos [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] Creating a new column with the number of the observation
Here is another approach: x - rep(c(A, B, C), c(3,1,2)) DF1 - data.frame(x) cbind(DF1, new_c=ave(as.numeric(DF1$x), DF1$x, FUN=function(x) 1:length(x))) Note the difference between DF (in previous solution) and DF1 str(DF) str(DF1) Andrija On Thu, May 29, 2014 at 12:03 AM, Andrija Djurovic djandr...@gmail.comwrote: Hi. Here is one approach: x - rep(c(A, B, C), c(3,1,2)) DF - data.frame(x, stringsAsFactors=FALSE) cbind(DF, new_c=c(lapply(rle(DF$x)[[1]], function(x) 1:x), recursive=T)) Andrija On Wed, May 28, 2014 at 10:46 PM, Marcos Santos mmsantos...@gmail.comwrote: I have a data frame like this: Category Observed_value A 100 A 130 A 140 B 90 C 80 D 120 D 130 I need to create an index column that show the number of the observation for each category. I have three observations in the category A, one in category B, one in category C and two in category D. The new matrix should be like this: Category Observed_value Index A 100 1 A 130 2 A 140 3 B 90 1 C 80 1 D 120 1 D 130 2 Please, could anyone help me with this problem? Thank you. -- Marcos Martins Santos mmsantos...@gmail.com -- Skype: marcosmartinssantos [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] adding a frequency variable to a data frame
Hi. Here is one solution: table(Donner$family)[Donner$family] Andrija On Fri, Apr 11, 2014 at 10:40 PM, Michael Friendly frien...@yorku.cawrote: I'm sure this is pretty simple, but it's Friday afternoon, and I just don't see it... In a data frame with a categorical/character factor, I want to add another column giving, for each observation, the frequency of that factor level. An example, where the variable of interest is family: data(Donner, package=vcdExtra) str(Donner) 'data.frame': 90 obs. of 5 variables: $ family : Factor w/ 10 levels Breen,Donner,..: 9 1 1 1 1 1 1 1 1 1 ... $ age : int 23 13 1 5 14 40 51 9 3 8 ... $ sex : Factor w/ 2 levels Female,Male: 2 2 1 2 2 1 2 2 2 2 ... $ survived: int 0 1 1 1 1 1 1 1 1 1 ... $ death : POSIXct, format: 1846-12-29 NA ... table(Donner$family) BreenDonner Eddy FosdWolfGraves Keseberg McCutchen MurFosPik 914 4 410 4 312 Other Reed 23 7 Here, I want to create a new variable, family.size, where all the Breens have 9, the Donners, 14, and so on... -- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. Chair, Quantitative Methods York University Voice: 416 736-2100 x66249 Fax: 416 736-5814 4700 Keele StreetWeb: http://www.datavis.ca Toronto, ONT M3J 1P3 CANADA __ R-help@r-project.org mailing list 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 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] adding a frequency variable to a data frame
another: ave(as.numeric(Donner$family), Donner$family, FUN=length) On Fri, Apr 11, 2014 at 10:51 PM, Andrija Djurovic djandr...@gmail.comwrote: Hi. Here is one solution: table(Donner$family)[Donner$family] Andrija On Fri, Apr 11, 2014 at 10:40 PM, Michael Friendly frien...@yorku.cawrote: I'm sure this is pretty simple, but it's Friday afternoon, and I just don't see it... In a data frame with a categorical/character factor, I want to add another column giving, for each observation, the frequency of that factor level. An example, where the variable of interest is family: data(Donner, package=vcdExtra) str(Donner) 'data.frame': 90 obs. of 5 variables: $ family : Factor w/ 10 levels Breen,Donner,..: 9 1 1 1 1 1 1 1 1 1 ... $ age : int 23 13 1 5 14 40 51 9 3 8 ... $ sex : Factor w/ 2 levels Female,Male: 2 2 1 2 2 1 2 2 2 2 ... $ survived: int 0 1 1 1 1 1 1 1 1 1 ... $ death : POSIXct, format: 1846-12-29 NA ... table(Donner$family) BreenDonner Eddy FosdWolfGraves Keseberg McCutchen MurFosPik 914 4 410 4 312 Other Reed 23 7 Here, I want to create a new variable, family.size, where all the Breens have 9, the Donners, 14, and so on... -- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. Chair, Quantitative Methods York University Voice: 416 736-2100 x66249 Fax: 416 736-5814 4700 Keele StreetWeb: http://www.datavis.ca Toronto, ONT M3J 1P3 CANADA __ R-help@r-project.org mailing list 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 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] From list to dataframe
Hi. Here is one way: l - list(structure(list(BP_A = c(27001689L, 27001689L, 27001689L, 27001689L, 27001689L, 27001689L), SNP_A = c(rs4822747, rs4822747, rs4822747, rs4822747, rs4822747, rs4822747), BP_B = c(27002392L, 27004298L, 27004902L, 27004964L, 27005122L, 27005158L), SNP_B = c(rs4820690, rs5761627, rs5752355, rs4822748, rs4820691, rs4822749 ), R2 = c(0.695642, 0.695642, 0.687861, 0.682715, 0.695642, 0.695642 )), .Names = c(BP_A, SNP_A, BP_B, SNP_B, R2), row.names = 2:7, class = data.frame), structure(list(BP_A = c(27002392L, 27002392L, 27002392L, 27002392L, 27002392L, 27002392L, 27002392L, 27002392L), SNP_A = c(rs4820690, rs4820690, rs4820690, rs4820690, rs4820690, rs4820690, rs4820690, rs4820690), BP_B = c(27001689L, 27004298L, 27004902L, 27004964L, 27005122L, 27005158L, 27005194L, 27005470L ), SNP_B = c(rs4822747, rs5761627, rs5752355, rs4822748, rs4820691, rs4822749, rs4822750, rs5761629), R2 = c(0.695642, 1, 0.988681, 0.988655, 1, 1, 0.988655, 0.695642)), .Names = c(BP_A, SNP_A, BP_B, SNP_B, R2), row.names = c(9L, 11L, 12L, 13L, 14L, 15L, 16L, 17L), class = data.frame), structure(list( BP_A = c(27004298L, 27004298L, 27004298L, 27004298L, 27004298L, 27004298L), SNP_A = c(rs5761627, rs5761627, rs5761627, rs5761627, rs5761627, rs5761627), BP_B = c(27001689L, 27002392L, 27004902L, 27004964L, 27005122L, 27005158L), SNP_B = c(rs4822747, rs4820690, rs5752355, rs4822748, rs4820691, rs4822749), R2 = c(0.695642, 1, 0.988681, 0.988655, 1, 1)), .Names = c(BP_A, SNP_A, BP_B, SNP_B, R2), row.names = c(19L, 20L, 22L, 23L, 24L, 25L), class = data.frame)) do.call(rbind, l) Andrija On Thu, Nov 14, 2013 at 9:41 AM, Hermann Norpois hnorp...@gmail.com wrote: Hello, having a list like testlist I would like to transform it in dataframe. How does it work? Thanks Hermann testlist [[1]] BP_A SNP_A BP_B SNP_B R2 2 27001689 rs4822747 27002392 rs4820690 0.695642 3 27001689 rs4822747 27004298 rs5761627 0.695642 4 27001689 rs4822747 27004902 rs5752355 0.687861 5 27001689 rs4822747 27004964 rs4822748 0.682715 6 27001689 rs4822747 27005122 rs4820691 0.695642 7 27001689 rs4822747 27005158 rs4822749 0.695642 [[2]] BP_A SNP_A BP_B SNP_B R2 9 27002392 rs4820690 27001689 rs4822747 0.695642 11 27002392 rs4820690 27004298 rs5761627 1.00 12 27002392 rs4820690 27004902 rs5752355 0.988681 13 27002392 rs4820690 27004964 rs4822748 0.988655 14 27002392 rs4820690 27005122 rs4820691 1.00 15 27002392 rs4820690 27005158 rs4822749 1.00 16 27002392 rs4820690 27005194 rs4822750 0.988655 17 27002392 rs4820690 27005470 rs5761629 0.695642 [[3]] BP_A SNP_A BP_B SNP_B R2 19 27004298 rs5761627 27001689 rs4822747 0.695642 20 27004298 rs5761627 27002392 rs4820690 1.00 22 27004298 rs5761627 27004902 rs5752355 0.988681 23 27004298 rs5761627 27004964 rs4822748 0.988655 24 27004298 rs5761627 27005122 rs4820691 1.00 25 27004298 rs5761627 27005158 rs4822749 1.00 #The aim is the dataframe BP_A SNP_A BP_B SNP_B R2 2 27001689 rs4822747 27002392 rs4820690 0.695642 3 27001689 rs4822747 27004298 rs5761627 0.695642 4 27001689 rs4822747 27004902 rs5752355 0.687861 5 27001689 rs4822747 27004964 rs4822748 0.682715 6 27001689 rs4822747 27005122 rs4820691 0.695642 7 27001689 rs4822747 27005158 rs4822749 0.695642 9 27002392 rs4820690 27001689 rs4822747 0.695642 11 27002392 rs4820690 27004298 rs5761627 1.00 12 27002392 rs4820690 27004902 rs5752355 0.988681 13 27002392 rs4820690 27004964 rs4822748 0.988655 14 27002392 rs4820690 27005122 rs4820691 1.00 15 27002392 rs4820690 27005158 rs4822749 1.00 16 27002392 rs4820690 27005194 rs4822750 0.988655 17 27002392 rs4820690 27005470 rs5761629 0.695642 19 27004298 rs5761627 27001689 rs4822747 0.695642 20 27004298 rs5761627 27002392 rs4820690 1.00 22 27004298 rs5761627 27004902 rs5752355 0.988681 23 27004298 rs5761627 27004964 rs4822748 0.988655 24 27004298 rs5761627 27005122 rs4820691 1.00 25 27004298 rs5761627 27005158 rs4822749 1.00 dput (testlist) list(structure(list(BP_A = c(27001689L, 27001689L, 27001689L, 27001689L, 27001689L, 27001689L), SNP_A = c(rs4822747, rs4822747, rs4822747, rs4822747, rs4822747, rs4822747), BP_B = c(27002392L, 27004298L, 27004902L, 27004964L, 27005122L, 27005158L), SNP_B = c(rs4820690, rs5761627, rs5752355, rs4822748, rs4820691, rs4822749 ), R2 = c(0.695642, 0.695642, 0.687861, 0.682715, 0.695642, 0.695642 )), .Names = c(BP_A, SNP_A, BP_B, SNP_B, R2), row.names = 2:7, class = data.frame), structure(list(BP_A = c(27002392L, 27002392L, 27002392L, 27002392L, 27002392L, 27002392L, 27002392L, 27002392L), SNP_A = c(rs4820690, rs4820690, rs4820690, rs4820690, rs4820690, rs4820690, rs4820690, rs4820690), BP_B = c(27001689L, 27004298L, 27004902L,
Re: [R] repeating values in an index two by two
Hi. Here are two approaches: c(mapply(function(x,y) rep(c(x,y), 2), (1:10)[c(T,F)], (1:10)[c(F,T)])) c(tapply(1:10, rep(1:(10/2), each=2), rep, 2), recursive=T) Andrija On Mon, Nov 11, 2013 at 1:11 PM, Federico Calboli f.calb...@imperial.ac.ukwrote: Hi All, I am trying to create an index that returns something like 1,2,1,2,3,4,3,4,5,6,5,6,7,8,7,8 and so on and so forth until a predetermined value (which is obviously even). I am trying very hard to avoid for loops or for loops front ends. I'd be obliged if anybody could offer a suggestion. BW F __ R-help@r-project.org mailing list 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 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] R function to locate Excel sheet?
Hi here is the solutions using XLConnect package: library(XLConnect) wb - loadWorkbook(path to your Excel file) c(particular sheet name)%in%getSheets(wb) Andrija On Tue, Oct 29, 2013 at 9:26 PM, Ron Michael ron_michae...@yahoo.comwrote: Hi, I am looking for some R function which will tell me, whether a particular sheet in an Excel file (.xlsx/.xls) exists or not. I just need to get some TRUE/FALSE type of answer. Can somebody give me any pointer if such function exists or not? Thanks and regards, __ R-help@r-project.org mailing list 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 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] resampling
Hi. See ?sample, ?replicate,?colMeans, ?plot.. Here is the simple example: sample(1:1000,20) replicate(5, sample(1:1000,20)) colMeans(replicate(5, sample(1:1000,20))) Andrija On Wed, Jul 31, 2013 at 1:23 PM, Rita Gamito rslo...@fc.ul.pt wrote: Could anyone tell me how,from a pool of 1002 observations (one variable), can I resample 1000 samples of 20 observations? And then calculate the mean and standard deviation between 2, 3, 4, ..., 1000 samples and plot them? Thank you! _ Rita Gamito Centro de Oceanografia Faculdade de Ciências, Universidade de Lisboa Campo Grande, 1749-016 Lisboa, Portugal e-mail: rgam...@fc.ul.pt Tel: + 351 21 750 00 00 - ext. 22575 Fax: + 351 21 750 02 07 www.co.fc.ul.pt __ R-help@r-project.org mailing list 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 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] calculate time from dates
Hi. See http://stackoverflow.com/questions/1995933/number-of-months-between-two-dates Andrija On Thu, Jul 11, 2013 at 11:56 AM, Gallon Li gallon...@gmail.com wrote: My data are from 2008 to 2010, with repeated measures for same subjects. I wish to compute number of months since january 2008. The data are like the following: ID date 1 4/12/2008 1 4/13/2008 1 5/11/2008 2 3/21/2009 2 4/22/2009 2 8/05/2009 ... the date column are in the format %m/%d/%y. i wish to obtain ID month 1 4 1 4 1 5 2 15 2 16 2 20 ... also, for the same ID with two identical month, I only want to keep the last one. can any expert help with this question? [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] Write an Excel workbook?
XLConnect package Andrija On Jun 27, 2013 9:15 PM, Spencer Graves spencer.gra...@structuremonitoring.com wrote: Hello: Is there a fully transportable way to write an Excel workbook? The writeFindFn2xls function in the sos package attempts to write search results to an Excel workbook consisting of (1) a summary of the packages identified in the search, (2) the individual help pages found, and (3) the search that produced 1 and 2. Unfortunately, for some time now, it only works with 32-bit R; the 64-bit R writes this information to three separate csv files. The current code first tries WriteXLS{WriteXLS}. Unfortunately, this fails on my computer, because it can't find the Perl module Text::CSV_XS. I don't see an easy fix to this. If WriteXLS fails, the code then tries odbcConnectExcel{ODBC}. From this, I get, odbcConnectExcel is only usable with 32-bit Windows. That's OK, but it means that I need to run all my searches on 32-bit R, and I usually use 64-bit R. I tried write.xls{dataframes2xls}. This put quotes around everything, which I found unacceptable. findFn('write.xls') identified a function by that name in the marray package. Unfortunately, install.packages(marray) produced, Warning message: package marray is not available (for R version 3.0.1) ???xls failed to produce anything else that looked to me like it would write a complete workbook. Suggestions? Thanks, Spencer Graves sessionInfo() R version 3.0.1 (2013-05-16) Platform: x86_64-w64-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=English_United States.1252 [2] LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] RODBC_1.3-6 WriteXLS_2.3.1 sos_1.3-7 brew_1.0-6 __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html 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 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] Choosing subset of data.frame
Hi. Try this: beta_results[beta_results$instrument%in%instru, ] and see help page ?%in% Hope this helps Andrija On Thu, Jun 20, 2013 at 12:45 PM, Katherine Gobin katherine_go...@yahoo.com wrote: Dear R Forum I have a data frame as beta_results = data.frame(instrument = c(ABC, DEF, JKL, LMN, PQR, STU, UVW, XYZ), beta_values = c(1.27, -0.22, 0.529, 0.011, 2.31, -1.08, -2.7, 0.42)) beta_results instrument beta_values 1ABC 1.270 2DEF -0.220 3JKL 0.529 4LMN 0.011 5PQR 2.310 6STU -1.080 7UVW -2.700 8XYZ 0.420 Through some other process, I am getting instrument names as say (which may change each time I run this process and hence I can't hard code it). instru = c(JKL, STU, XYZ) Now I want the subset of beta_results, (say beta_results_A) pertaining to only instru i.e beta_results_A = instrument beta_values 3JKL 0.529 6STU -1.080 8XYZ 0.420 I did try beta_results_A = beta_results[instru] or beta_results_A = subset(beta_results, beta_results$instrument = instru] but I guess it's failing. Kindly guide Regards Katherine [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] Trouble sorting the matrix
Hi. Here is an example of sorting matrix columns: mat - matrix(10:1, ncol=2, byrow=TRUE) mat [,1] [,2] [1,] 109 [2,]87 [3,]65 [4,]43 [5,]21 apply(mat, 2, function(x) x[order(x)]) [,1] [,2] [1,]21 [2,]43 [3,]65 [4,]87 [5,] 109 You should read help page [, apply and order (?[, ?apply, ?order) Hope this helps Andrija On Sat, Jun 15, 2013 at 12:36 PM, Ola Cabaj olaca...@gmail.com wrote: I would like to sort matrix3, so that every column in output matrix is sorted. Also I have to unsort it later on. matrix1-matrix(rnorm(100,354,78),ncol=10) matrix2-matrix(rnorm(100,225,102),ncol=10) matrix3-cbind(matrix1,matrix2) nrCol-length(matrix3[1,]) class1-1:10 for(i in 1:nrCol) { sorted.matrix-matrix3[order(matrix3[class1,i]),] } for(i in 1:liczbaKolumn) { output-sorted.matrix[order(matrix3[class1,i]),] } -- Aleksandra Cabaj [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] SQL queries in R
?sqlQuery as.is - argument Andrija On Jun 7, 2013 9:10 PM, Sneha Bishnoi sneha.bish...@gmail.com wrote: Hey all! I am trying to select a bunch of id's (data type -character) from a table and store them in a variable in R But when i do this, it automatically truncates the leading zero's in id's even though they are of character type. code is :- myconn-odbcConnect(testdata) sql.select-paste(select UNIT_ID from UNITS where (UNIT_TYPE=',unit,' and COMMUNITY=',property,'),sep=) unit_ids-sqlQuery(myconn,sql.select) print(unit_ids) is there anyway i can retain the UNIT_ID's as they are. Thanks! Sneha [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] SQL queries in R
myconn-odbcConnect(testdata) sql.select-paste(select UNIT_ID from UNITS where (UNIT_TYPE=',unit,' and COMMUNITY=',property,'),sep=) unit_ids-sqlQuery(myconn,sql.select,as.is=TRUE) This should works if myconn and sql.select are defined properly Andrija On Jun 7, 2013 9:58 PM, Sneha Bishnoi sneha.bish...@gmail.com wrote: tried as.is ,gives an error, [1] 01000 10054 [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionWrite (send()). [2] [RODBC] ERROR: Could not SQLExecDirect 'select UNIT_ID from UNITS where (UNIT_TYPE='1X1' and COMMUNITY='SAN1193')' On Fri, Jun 7, 2013 at 3:21 PM, andrija djurovic djandr...@gmail.comwrote: ?sqlQuery as.is - argument Andrija On Jun 7, 2013 9:10 PM, Sneha Bishnoi sneha.bish...@gmail.com wrote: Hey all! I am trying to select a bunch of id's (data type -character) from a table and store them in a variable in R But when i do this, it automatically truncates the leading zero's in id's even though they are of character type. code is :- myconn-odbcConnect(testdata) sql.select-paste(select UNIT_ID from UNITS where (UNIT_TYPE=',unit,' and COMMUNITY=',property,'),sep=) unit_ids-sqlQuery(myconn,sql.select) print(unit_ids) is there anyway i can retain the UNIT_ID's as they are. Thanks! Sneha [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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. -- Sneha Bishnoi +14047235469 H. Milton Stewart School of Industrial Systems Engineering Georgia Tech [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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] numeric not equal its value
Hi. Check R FAQ, section 7.31 Why doesn't R think these numbers are equal? http://cran.r-project.org/doc/FAQ/R-FAQ.html all.equal(a,b) Andrija On Mon, May 20, 2013 at 2:36 PM, Jannis bt_jan...@yahoo.de wrote: Dear R users, I ran into the strange situation where a number does not seem to equal its value. Try this: a - seq(0.05,0.7,0.05)[3] b - 0.15 a == b Should this not be TRUE? a-b yields a very small number (and not 0) so this most probably is a numerical error, but why does seq create such numbers? Thanks a lot Jannis __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html 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 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] Questions on function readNamedRegionFromFile in XLConnect pacakge
Hi Miao. 1. Calendar, Iris, IQ are the named region in excel file multiregion.xlsx (demoFiles/multiregion.xlsx). Open this file (demoFiles/multiregion.xlsx), go to Formulas tab and click Name manager to see reference regions. 2. Check following functions: ?readWorksheet #arguments startCol, startRow, endCol, endRow ?idx2col ?col2idx Example: col2idx(AK) [1] 37 idx2col(37) [1] AK Hope this helps. Andrija On Tue, Apr 23, 2013 at 8:49 AM, jpm miao miao...@gmail.com wrote: Hi, I have two questions on the function readNamedRegionFromFile in XLConnect pacakge. 1. In the documentation, # multiregion xlsx file from demoFiles subfolder of package XLConnect demoExcelFile - system.file(demoFiles/multiregion.xlsx, package = XLConnect) # Load a single named region into a single data.frame. df - readNamedRegionFromFile(demoExcelFile, name=Iris) # Load multiple regions at once - returns a (named) list # of data.frames. df - readNamedRegionFromFile(demoExcelFile, name=c(Calendar, Iris, IQ)) What are the names Calendar, Iris, IQ? I just couldn't find them from the file. 2. Since my xlsx file is big, I might want to read data like AK9:AK18, for example. If my computation is right, AK is the 37th column. Then I need to tell the computer to read the 37th column, row 9 to row 18. Can I ask the function (any function in the package) to read the column by the header name (say, the name saved in AK1) instead of 37th? Thanks, Miao [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] Questions on function readNamedRegionFromFile in XLConnect pacakge
Here is the concrete example: # multiregion xlsx file from demoFiles subfolder of package XLConnect demoExcelFile - system.file(demoFiles/multiregion.xlsx, + package = XLConnect) # Load a single named region into a single data.frame. DF1 - readNamedRegionFromFile(demoExcelFile, name=Iris) DF2 - readWorksheetFromFile(demoExcelFile, sheet=SecondSheet, + startCol=col2idx(B), + endCol=col2idx(F), + startRow=3, +header=TRUE) head(DF1) Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2setosa 2 4.9 3.0 1.4 0.2setosa 3 4.7 3.2 1.3 0.2 barbarica 4 4.6 3.1 1.5 0.2setosa 5 5.0 3.6 1.4 0.2setosa 6 5.4 3.9 1.7 0.4setosa head(DF2) Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2setosa 2 4.9 3.0 1.4 0.2setosa 3 4.7 3.2 1.3 0.2 barbarica 4 4.6 3.1 1.5 0.2setosa 5 5.0 3.6 1.4 0.2setosa 6 5.4 3.9 1.7 0.4setosa identical(DF1, DF2) [1] TRUE Andrija On Tue, Apr 23, 2013 at 9:04 AM, andrija djurovic djandr...@gmail.comwrote: Hi Miao. 1. Calendar, Iris, IQ are the named region in excel file multiregion.xlsx (demoFiles/multiregion.xlsx). Open this file (demoFiles/multiregion.xlsx), go to Formulas tab and click Name manager to see reference regions. 2. Check following functions: ?readWorksheet #arguments startCol, startRow, endCol, endRow ?idx2col ?col2idx Example: col2idx(AK) [1] 37 idx2col(37) [1] AK Hope this helps. Andrija On Tue, Apr 23, 2013 at 8:49 AM, jpm miao miao...@gmail.com wrote: Hi, I have two questions on the function readNamedRegionFromFile in XLConnect pacakge. 1. In the documentation, # multiregion xlsx file from demoFiles subfolder of package XLConnect demoExcelFile - system.file(demoFiles/multiregion.xlsx, package = XLConnect) # Load a single named region into a single data.frame. df - readNamedRegionFromFile(demoExcelFile, name=Iris) # Load multiple regions at once - returns a (named) list # of data.frames. df - readNamedRegionFromFile(demoExcelFile, name=c(Calendar, Iris, IQ)) What are the names Calendar, Iris, IQ? I just couldn't find them from the file. 2. Since my xlsx file is big, I might want to read data like AK9:AK18, for example. If my computation is right, AK is the 37th column. Then I need to tell the computer to read the 37th column, row 9 to row 18. Can I ask the function (any function in the package) to read the column by the header name (say, the name saved in AK1) instead of 37th? Thanks, Miao [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] Automation of R input
Hi. If you want to do it completely in R you can create R gui using gWidgets package. Here is an example of creating gui for function arguments (thanks to John Verzani): library(gWidgets) options(guiToolkit=tcltk) ##' An editor for an argument ##' depends on calss of object arg_editor - function(x, cont, ...) UseMethod(arg_editor) arg_editor.call - function(x, cont, ...) arg_editor(eval(x), cont, ...) arg_editor.name - function(x, cont, ...) arg_editor(eval(x), cont, ...) arg_editor.numeric - function(x, cont, ...) gcombobox(sort(unique(x)), editable=TRUE, coerce.with=as.numeric, cont=cont) arg_editor.character - function(x, cont, ...) gcombobox(sort(unique(x)), editable=TRUE, coerce.with=as.character, cont=cont) arg_editor.factor - function(x, cont, ...) gcombobox(sort(unique(x)), cont=cont) arg_editor.logical - function(x, cont, ...) gcombobox(c(TRUE, FALSE), selected=2 - as.numeric(x[1]), cont=cont) ## This replaces ggenericwidget make_gui - function(FUN, parent) { args - formals(FUN) if(missing(parent)) { parent - gwindow(Some GUI, visible=FALSE) } g - ggroup(cont=parent, horizontal=FALSE) lyt - glayout(cont=g) n - length(args) f - function(i, nm, x) { lyt[i,1] - nm lyt[i,2] - arg_editor(args[[i]], cont=lyt) } mapply(f, i=seq_along(args), nm=names(args), x=args) bg - ggroup(cont=g) gbutton(ok, cont=bg, handler=function(h, ...) { values - lapply(lyt[,2], svalue) nms - sapply(lyt[,1], svalue) names(values) - nms do.call(FUN, values) dispose(parent) }) visible(parent) - TRUE invisible(lyt) } ## Try it out s - function(x=1:4, y = letters, z=TRUE) { print(list(x, y, z)) assign(x,x, envir=globalenv()) assign(y,y, envir=globalenv()) assign(z,z, envir=globalenv()) } make_gui(s) You can easily adjust this example to your needs. If you want you can use Excel, Access in order to create front end application and using commandArgs to supply arguments to an R script when is invoked. Examples of calling an R scripts from Excel and Access you can find here: http://www.slideshare.net/djandrija/excelr http://www.slideshare.net/djandrija/r-and-access-2007 Also, shiny package can be useful here (http://www.rstudio.com/shiny/), integration of html, php and R... Hope this helps. Andrija On Tue, Apr 23, 2013 at 9:41 AM, Vahe nr vne...@gmail.com wrote: Hi all, I have R script which during its run require an input like this: choose between one of the grouping factor available : c(Village, Country) can I automate this part, in other word to pass for example Village when I am running the script. One more thing the script is the TimeSeriesAnalysis {ndvits}. Thanks in advance for any help or suggestion. Regards, Vahe [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] Using different function (parameters) with apply
Hi. It would be easier to help you if you show us the result you are trying to obtain. Here are my attempts: sweep(a,2,div, FUN=/) [,1] [,2] [,3] [1,]1 2.0 2.33 [2,]2 2.5 2.67 [3,]3 3.0 3.00 or a/matrix(rep(div,3),ncol=3, byrow=TRUE) [,1] [,2] [,3] [1,]1 2.0 2.33 [2,]2 2.5 2.67 [3,]3 3.0 3.00 Hope this helps. Andrija On Thu, Apr 18, 2013 at 7:20 AM, Sachinthaka Abeywardana sachin.abeyward...@gmail.com wrote: Hi All, I have the following problem (read the commented bit below): a-matrix(1:9,nrow=3) a [,1] [,2] [,3] [1,]147 [2,]258 [3,]369 div-1:3 apply(a,2,function(x)x/div) ##want to divide each column by div- instead each row is divided## [,1] [,2] [,3] [1,]1 4.07 [2,]1 2.54 [3,]1 2.03 apply(a,1,function(x)x/div) ##Changing Margin from 2 to 1 does something completele weird## [,1] [,2] [,3] [1,] 1.00 2.003 [2,] 2.00 2.503 [3,] 2.33 2.673 Any thoughts? Thanks, Sachin [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] How can I ask R to skip the title when reading the data?
Hi. You can try with argument skip in read.csv function(check ?read.csv). Also, if you want directly to import an Excel file you can use readWorksheet function from XLConnect package and use argument startRow to set up the first row to read from. Hope this helps. Andrija On Thu, Apr 18, 2013 at 9:46 AM, jpm miao miao...@gmail.com wrote: I have many xls grade report sheets with the same format XXX High School Grade Report Confidential Yes Math English Science John 90 85 90 Mary 75 88 93 Since the reports are prepared on a regular basis, I have many reports with identical format. Without the title XXX High School Grade Report Confidential Yes, I can just change the xls to csv and read them via read.csv. How can I read the data in the presence of the title? How can I ask R to skip the title and just read the data? Thanks, Miao [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] Subsetting a large number into smaller numbers and find the largest product
Hi. Here is one approach: options(scipen=300) numb - 73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557 strsplit(as.character(numb), ) blocks - rep(1:(nchar(numb)/5), each=5) tapply(as.numeric(unlist(strsplit(as.character(numb), ))), blocks, prod) Andrija On Thu, Apr 18, 2013 at 10:47 AM, Janesh Devkota janesh.devk...@gmail.comwrote: Hello, I have a big number lets say of around hundred digits. I want to subset that big number into consecutive number of 5 digits and find the product of those 5 digits. For example my first 5 digit number would be 73167. I need to check the product of the individual numbers in 73167 and so on. The sample number is as follows: 73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557 I have a problem subsetting the small numbers out of the big number. Any help is highly appreciated. Best Regards, Janesh Devkota [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] dividing a long column to many short ones by a condition
Hi Igor. Here is one way: DF - read.table(textConnection(90.1194354 87.94788274 80.34744843 64.06080347 30.40173724 0 0 0 0 0 16.28664495 23.88707926 29.31596091 48.85993485 13.02931596 0 0 0 7.600434311 20.62975027 29.31596091 32.5732899), header=FALSE) a - DF$V1[which(DF$V1!=0)] indx - which(DF$V1!=0) blocks - cut(1:length(a), breaks=c(0,which(diff(indx)!=1), length(a))) n_levels - length(levels(blocks)) l - vector(list, n_levels) for(i in 1:n_levels) { l[[i]] - a[blocks==levels(blocks)[i]] } l Andrija On Thu, Apr 18, 2013 at 11:33 AM, Igor Mintz igormi...@gmail.com wrote: hello i have a very long column of numbers. i want R to make a new column every time the value changes from zero. example for the column: 90.1194354 87.94788274 80.34744843 64.06080347 30.40173724 0 0 0 0 0 16.28664495 23.88707926 29.31596091 48.85993485 13.02931596 0 0 0 7.600434311 20.62975027 29.31596091 32.5732899 for this example i want to get 3 columns. thanks! [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] dividing a long column to many short ones by a condition
Hi. I completely forgot split function so loop: l - vector(list, n_levels) for(i in 1:n_levels) { l[[i]] - a[blocks==levels(blocks)[i]] } l could be substitute with: split(a, blocks), but anyway Rui's solution is better. Andrija On Thu, Apr 18, 2013 at 1:18 PM, andrija djurovic djandr...@gmail.comwrote: Hi Igor. Here is one way: DF - read.table(textConnection(90.1194354 87.94788274 80.34744843 64.06080347 30.40173724 0 0 0 0 0 16.28664495 23.88707926 29.31596091 48.85993485 13.02931596 0 0 0 7.600434311 20.62975027 29.31596091 32.5732899), header=FALSE) a - DF$V1[which(DF$V1!=0)] indx - which(DF$V1!=0) blocks - cut(1:length(a), breaks=c(0,which(diff(indx)!=1), length(a))) n_levels - length(levels(blocks)) l - vector(list, n_levels) for(i in 1:n_levels) { l[[i]] - a[blocks==levels(blocks)[i]] } l Andrija On Thu, Apr 18, 2013 at 11:33 AM, Igor Mintz igormi...@gmail.com wrote: hello i have a very long column of numbers. i want R to make a new column every time the value changes from zero. example for the column: 90.1194354 87.94788274 80.34744843 64.06080347 30.40173724 0 0 0 0 0 16.28664495 23.88707926 29.31596091 48.85993485 13.02931596 0 0 0 7.600434311 20.62975027 29.31596091 32.5732899 for this example i want to get 3 columns. thanks! [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] Creating a vector with repeating dates
?rep On Wed, Apr 17, 2013 at 11:11 AM, Katherine Gobin katherine_go...@yahoo.com wrote: Dear R forum I have a data.frame df = data.frame(dates = c(4/15/2013, 4/14/2013, 4/13/2013, 4/12/2013), values = c(47, 38, 56, 92)) I need to to create a vector by repeating the dates as Current_date, 4/15/2013, 4/14/2013, 4/13/2013, 4/12/2013, Current_date, 4/15/2013, 4/14/2013, 4/13/2013, 4/12/2013, Current_date, 4/15/2013, 4/14/2013, 4/13/2013, 4/12/2013 i.e. I need to create a new vector as given below which I need to use for some other purpose. Current_date 4/15/2013 4/14/2013 4/13/2013 4/12/2013 Current_date 4/15/2013 4/14/2013 4/13/2013 4/12/2013 Current_date 4/15/2013 4/14/2013 4/13/2013 4/12/2013 Is it possible to construct such a column? Regards Katherine [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] Creating a vector with repeating dates
Hi. Here are some examples that can maybe help you: a - Current date b - Sys.Date()-1:5 a b class(a) class(b) c(a,b) mode(b) as.numeric(b) class(c(a,b)) c(a, as.character(b)) class(c(a,b)) class(c(a,as.character(b))) Hope this helps. On Wed, Apr 17, 2013 at 11:21 AM, Katherine Gobin katherine_go...@yahoo.com wrote: Dear Andrija Djurovic, Thanks for the suggestion. Ia m aware of rep. However, here I need to repeat not only dates, but a string Current_date. Thus, I need to create a vector ( to be included in some other data.frame) with the name say dt which will contain dt Current_date 4/15/2013 4/14/2013 4/13/2013 4/12/2013 Current_date 4/15/2013 4/14/2013 4/13/2013 4/12/2013 Current_date 4/15/2013 4/14/2013 4/13/2013 4/12/2013 So this is combination of dates and a string. Hence, I am just wondering if it is possible to create such a vector or not? Regards Katherine --- On *Wed, 17/4/13, andrija djurovic djandr...@gmail.com* wrote: From: andrija djurovic djandr...@gmail.com Subject: Re: [R] Creating a vector with repeating dates To: Katherine Gobin katherine_go...@yahoo.com Cc: r-help@r-project.org r-help@r-project.org Date: Wednesday, 17 April, 2013, 10:14 AM ?rep On Wed, Apr 17, 2013 at 11:11 AM, Katherine Gobin katherine_go...@yahoo.com http://mc/compose?to=katherine_go...@yahoo.com wrote: Dear R forum I have a data.frame df = data.frame(dates = c(4/15/2013, 4/14/2013, 4/13/2013, 4/12/2013), values = c(47, 38, 56, 92)) I need to to create a vector by repeating the dates as Current_date, 4/15/2013, 4/14/2013, 4/13/2013, 4/12/2013, Current_date, 4/15/2013, 4/14/2013, 4/13/2013, 4/12/2013, Current_date, 4/15/2013, 4/14/2013, 4/13/2013, 4/12/2013 i.e. I need to create a new vector as given below which I need to use for some other purpose. Current_date 4/15/2013 4/14/2013 4/13/2013 4/12/2013 Current_date 4/15/2013 4/14/2013 4/13/2013 4/12/2013 Current_date 4/15/2013 4/14/2013 4/13/2013 4/12/2013 Is it possible to construct such a column? Regards Katherine [[alternative HTML version deleted]] __ R-help@r-project.org http://mc/compose?to=R-help@r-project.org mailing list 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 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] speedometer charts in R
Hi. Thanks once more for helping me out. Beside that you gave me a nice idea and here is the sample code of dashboard for an R-powered race car that you can't wait to see :) library(animation) library(jpeg) # Original code by Gaston Sanchez # http://www.r-bloggers.com/gauge-chart-in-r/ # Modified by Jeff Hemsley dial.plot - function(label = UseR!, value = 78, dial.radius = 1 , value.cex = 3, value.color = black , label.cex = 3, label.color = black , gage.bg.color = white , yellowFrom = 75, yellowTo = 90, yellow.slice.color = #FF9900 , redFrom = 90, redTo = 100, red.slice.color = #DC3912 , needle.color = red, needle.center.color = black, needle.center.cex = 1 , dial.digets.color = grey50 , heavy.border.color = gray85, thin.border.color = gray20, minor.ticks.color = gray55, major.ticks.color = gray45) { whiteFrom = min(yellowFrom, redFrom) - 2 whiteTo = max(yellowTo, redTo) + 2 # function to create a circle circle - function(center=c(0,0), radius=1, npoints=100) { r = radius tt = seq(0, 2*pi, length=npoints) xx = center[1] + r * cos(tt) yy = center[1] + r * sin(tt) return(data.frame(x = xx, y = yy)) } # function to get slices slice2xy - function(t, rad) { t2p = -1 * t * pi + 10*pi/8 list(x = rad * cos(t2p), y = rad * sin(t2p)) } # function to get major and minor tick marks ticks - function(center=c(0,0), from=0, to=2*pi, radius=0.9, npoints=5) { r = radius tt = seq(from, to, length=npoints) xx = center[1] + r * cos(tt) yy = center[1] + r * sin(tt) return(data.frame(x = xx, y = yy)) } # external circle (this will be used for the black border) border_cir = circle(c(0,0), radius=dial.radius, npoints = 100) # open plot plot(border_cir$x, border_cir$y, type=n, asp=1, axes=FALSE, xlim=c(-1.05,1.05), ylim=c(-1.05,1.05), xlab=, ylab=) # gray border circle external_cir = circle(c(0,0), radius=( dial.radius * 0.97 ), npoints = 100) # initial gage background polygon(external_cir$x, external_cir$y, border = gage.bg.color, col = gage.bg.color, lty = NULL) # add gray border lines(external_cir$x, external_cir$y, col=heavy.border.color, lwd=18) # add external border lines(border_cir$x, border_cir$y, col=thin.border.color, lwd=2) # yellow slice (this will be used for the yellow band) yel_ini = (yellowFrom/100) * (12/8) yel_fin = (yellowTo/100) * (12/8) Syel = slice2xy(seq.int(yel_ini, yel_fin, length.out = 30), rad= (dial.radius * 0.9) ) polygon(c(Syel$x, 0), c(Syel$y, 0), border = yellow.slice.color, col = yellow.slice.color, lty = NULL) # red slice (this will be used for the red band) red_ini = (redFrom/100) * (12/8) red_fin = (redTo/100) * (12/8) Sred = slice2xy(seq.int(red_ini, red_fin, length.out = 30), rad= (dial.radius * 0.9) ) polygon(c(Sred$x, 0), c(Sred$y, 0), border = red.slice.color, col = red.slice.color, lty = NULL) # white slice (this will be used to get the yellow and red bands) white_ini = (whiteFrom/100) * (12/8) white_fin = (whiteTo/100) * (12/8) Swhi = slice2xy(seq.int(white_ini, white_fin, length.out = 30), rad= (dial.radius * 0.8) ) polygon(c(Swhi$x, 0), c(Swhi$y, 0), border = gage.bg.color, col = gage.bg.color, lty = NULL) # calc and plot minor ticks minor.tix.out - ticks(c(0,0), from=5*pi/4, to=-pi/4, radius=( dial.radius * 0.89 ), 21) minor.tix.in - ticks(c(0,0), from=5*pi/4, to=-pi/4, radius=( dial.radius * 0.85 ), 21) arrows(x0=minor.tix.out$x, y0=minor.tix.out$y, x1=minor.tix.in$x, y1= minor.tix.in$y, length=0, lwd=2.5, col=minor.ticks.color) # coordinates of major ticks (will be plotted as arrows) major_ticks_out = ticks(c(0,0), from=5*pi/4, to=-pi/4, radius=( dial.radius * 0.9 ), 5) major_ticks_in = ticks(c(0,0), from=5*pi/4, to=-pi/4, radius=( dial.radius * 0.77 ), 5) arrows(x0=major_ticks_out$x, y0=major_ticks_out$y, col=major.ticks.color, x1=major_ticks_in$x, y1=major_ticks_in$y, length=0, lwd=3) # calc and plot numbers at major ticks dial.numbers - ticks(c(0,0), from=5*pi/4, to=-pi/4, radius=( dial.radius * 0.70 ), 5) dial.lables - c(0, 25, 50, 75, 100) text(dial.numbers$x, dial.numbers$y, labels=dial.lables, col=dial.digets.color, cex=.8) # Add dial lables text(0, (dial.radius * -0.65), value, cex=value.cex, col=value.color) # add label of variable text(0, (dial.radius * 0.43), label, cex=label.cex, col=label.color) # add needle # angle of needle pointing to the specified value val = (value/100) * (12/8) v = -1 * val * pi + 10*pi/8 # 10/8 becuase we are drawing on only %80 of the cir # x-y coordinates of needle needle.length - dial.radius * .67 needle.end.x = needle.length * cos(v) needle.end.y = needle.length * sin(v) needle.short.length - dial.radius * .1 needle.short.end.x = needle.short.length * -cos(v) needle.short.end.y =
Re: [R] speedometer charts in R
Hi. Thanks for help. In meanwhile some of contributors already send me the same link with examples. I agree with you and I am not trying to drive a race car just to do what was asked from me :) Andrija On Wed, Apr 3, 2013 at 11:24 AM, Barry Rowlingson b.rowling...@lancaster.ac.uk wrote: On Tue, Apr 2, 2013 at 4:00 PM, R. Michael Weylandt michael.weyla...@gmail.com wrote: Look at the R GoogleVis package. Or read what Hadley W had to say on a similar question first: The question would why would you want to? You are trying to understand your data, not driving a race car or aeroplane. - http://r.789695.n4.nabble.com/Graphical-output-dials-and-meters-for-a-dashboard-td845090.html But maybe you *are* creating a dashboard for an R-powered race car, in which case here's an R-native version of the google vis speedometers: http://gastonsanchez.wordpress.com/2013/01/10/gauge-chart-in-r/ Can't wait to see the full source code for your race car: require(engine) block = engine(cc=2000,cylinders=6) require(downforce) ... Barry [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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] speedometer charts in R
Hi useRs. Does anybody know if there is some function that creates speedometer chart in R? Or if anybody has proposals where to start looking and which functions I can modify in order to create this kind of chart? Thanks for any help Andrija [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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] highlight overlapping region of two densities
Hi all. I would like to highlight overlapping regions of two densities and I could not find a way to do it. Here is the sample code: myd - c(2,4,5, 4,3,2,2,3,3,3,2,3,3,4,2,4,3,3,3,2,2.5, 2, 3,3, 2.3, 3, 3, 2, 3) myd1 - myd-2 plot(range(density(myd)$x, density(myd1)$x), range(density(myd)$y, density(myd1)$y), type = n) lines(density(myd), col=1, lwd=4) lines(density(myd1), col=2, lwd=4, lty=2) So, I am trying to highlight the region from 1 to 4, on x axis, taking the minimum value of corresponding y values. I am aware of polygon function but don't know how to define correctly x and y coordinate vectors for this function. Could someone help me to solve this? Thank in advance. Andrija [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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] highlight overlapping region of two densities
Hi Pierrick, thanks for reply. This is the closest solution that I got. I also found something like this: dens1 - density(myd) dens2 - density(myd1) plot(range(dens1$x, dens2$x), range(dens1$y, dens2$y), type = n) polygon(dens1$x, dens1$y, col = rgb(1,0,0, .5),lwd=4, lty=2) polygon(dens2$x, dens2$y, col = rgb(0,0,1, .5),lwd=4) Your solution is closer to what I want but also solution above can be, maybe, useful. Thank you Andrija On Wed, Mar 20, 2013 at 2:20 PM, Pierrick Bruneau pbrun...@gmail.comwrote: Hi Andrija, As far as I understood, your problem comes from the density() function - the domains of myd and myd1 are different, which causes slightly shifted x values in the density() output. A simple workaround (which you maybe already came up with): dens1 - density(myd, from=-1, to=6) # force both densities on same domains, dens2 - density(myd1, from=-1, to=6) # which facilitates drawing plot(range(dens1$x, dens2$x), range(dens1$y, dens2$y), type = n) lines(dens1, col=1, lwd=4) lines(dens2, col=2, lwd=4, lty=2) polygon(dens1$x, pmin(dens1$y, dens2$y), col=grey) But doing so causes inelegant distribution tails... Maybe somebody will have a more elegant suggestion ? Pierrick Bruneau CRP Gabriel Lippmann On Wed, Mar 20, 2013 at 1:32 PM, andrija djurovic djandr...@gmail.comwrote: Hi all. I would like to highlight overlapping regions of two densities and I could not find a way to do it. Here is the sample code: myd - c(2,4,5, 4,3,2,2,3,3,3,2,3,3,4,2,4,3,3,3,2,2.5, 2, 3,3, 2.3, 3, 3, 2, 3) myd1 - myd-2 plot(range(density(myd)$x, density(myd1)$x), range(density(myd)$y, density(myd1)$y), type = n) lines(density(myd), col=1, lwd=4) lines(density(myd1), col=2, lwd=4, lty=2) So, I am trying to highlight the region from 1 to 4, on x axis, taking the minimum value of corresponding y values. I am aware of polygon function but don't know how to define correctly x and y coordinate vectors for this function. Could someone help me to solve this? Thank in advance. Andrija [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] highlight overlapping region of two densities
Hi Rui. Thank you very much. I had similar idea like yours but your is more elegant. Andrija On Wed, Mar 20, 2013 at 4:03 PM, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, Try the following. d0 - density(myd) d1 - density(myd1) idx0 - d0$x = 1 d0$x = 4 idx1 - d1$x = 1 d1$x = 4 yy - apply(cbind(d0$y[idx0], d1$y[idx1]), 1, min) xx - d0$x[idx0] xx - c(xx[1], xx, xx[1]) yy - c(0, yy, 0) polygon(xx, yy, col = blue) Hope this helps, Rui Barradas Em 20-03-2013 12:32, andrija djurovic escreveu: Hi all. I would like to highlight overlapping regions of two densities and I could not find a way to do it. Here is the sample code: myd - c(2,4,5, 4,3,2,2,3,3,3,2,3,3,4,2,4,3,3,**3,2,2.5, 2, 3,3, 2.3, 3, 3, 2, 3) myd1 - myd-2 plot(range(density(myd)$x, density(myd1)$x), range(density(myd)$y, density(myd1)$y), type = n) lines(density(myd), col=1, lwd=4) lines(density(myd1), col=2, lwd=4, lty=2) So, I am trying to highlight the region from 1 to 4, on x axis, taking the minimum value of corresponding y values. I am aware of polygon function but don't know how to define correctly x and y coordinate vectors for this function. Could someone help me to solve this? Thank in advance. Andrija [[alternative HTML version deleted]] __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html 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 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] top 10 (n values) for each classes
Hi, see function ?head or [. If DF is you data frame, top n values of DF you can select with head(DF, n) or DF[1:n, ]... Andrija On Sun, Feb 10, 2013 at 11:31 AM, catalin roibu catalinro...@gmail.comwrote: Dear R users, I have a problem. I don't know how to select the top 10 (n) values for each classes. Thank you! My data is like this: row.names proc cls 7271 568,03338 0,5 7270 554,68458 0,5 7269 510,20638 0,5 7268 485,59969 0,5 7267 421,92852 0,5 7272 410,12101 0,5 3414 409,71429 0,5 3452 402,78699 0,5 3451 401,28114 0,5 3450 361,80607 0,5 3413 360,29883 0,5 3449 352,90043 0,5 3453 339,36457 0,5 7266 331,38278 0,5 3415 327,89374 0,5 3454 324,4335 0,5 3412 291,95631 0,5 4770 274,12649 0,5 3455 274,09326 0,5 6030 264,72652 0,5 4772 264,17632 0,5 6031 261,19403 0,5 3411 255,50239 0,5 4773 241,50886 0,5 3447 237,78135 0,5 3448 232,9554 0,5 4774 232,92985 0,5 7273 229,18868 0,5 6028 225,24249 0,5 11839 220,61201 0,5 4771 218,94856 0,5 6026 218,7 0,5 11838 213,44438 0,5 3410 209,35622 0,5 6027 206,68399 0,5 6029 206,10424 0,5 11837 205,28621 0,5 6025 201,21227 0,5 1042 199,78883 0,5 4775 197,29348 1 7265 195,07017 1 4776 175,90948 1 10725 175,60525 1 10724 173,2342 1 3456 172,44641 1 11589 170,2784 1 7190 169,53567 1 11799 169,19097 1 6024 169,09314 1 3416 168,39827 1 10726 165,25617 1 11840 164,43032 1 11800 162,7957 1 11590 155,31572 1 1043 154,6438 1 11841 153,80017 1 7189 152,26293 1 11588 151,07692 1 11798 151,0658 1 3446 147,0405 1 11836 147,02381 1 7188 142,01954 1 11835 140,95302 1 5994 138,06452 1 3523 137,98974 1 11797 137,57633 1 11728 137,49488 1 7192 136,87805 1 11731 135,10162 1 5995 132,41401 1 3096 131,11658 1 7191 131 1 3525 128,74521 1 11801 128,57672 1 7264 128,24427 1 3099 128,20339 1,5 11377 127,53474 1,5 10948 126,81159 1,5 11834 126,67371 1,5 11378 126,42474 1,5 11842 126,27561 1,5 4777 126,26175 1,5 10949 124,39545 1,5 3524 123,64685 1,5 5993 123,39782 1,5 10943 123,27824 1,5 3409 122,84964 1,5 10942 121,28378 1,5 11730 118,13305 1,5 10702 117,91925 1,5 11796 117,5239 1,5 4769 117,38123 1,5 11587 116,49326 1,5 7223 114,19009 1,5 3396 114,16026 1,5 3097 113,99856 1,5 3526 113,97446 1,5 11376 113,58453 1,5 11591 113,46819 1,5 7163 110,99291 1,5 1044 110,76636 1,5 3457 110,35221 1,5 10944 109,87002 1,5 11802 109,64554 1,5 3100 109,2207 1,5 6023 109,01308 1,5 10947 108,45018 1,5 3098 108,20547 1,5 11593 108,0705 1,5 3458 108,03632 1,5 11592 107,53228 1,5 11729 107,35615 1,5 3095 107,24396 1,5 7222 106,02489 1,5 10945 105,24515 1,5 3101 104,75914 1,5 10946 103,92964 1,5 5199 103,81953 1,5 11832 103,6405 1,5 11333 103,53049 1,5 7220 102,87019 1,5 11379 102,66542 1,5 3104 102,5464 1,5 10941 101,4321 1,5 5198 100,62386 1,5 11375 99,731904 1,5 11803 99,08347 1,5 7187 98,076923 1,5 7221 96,853367 1,5 11356 96,797409 1,5 7167 96,326062 1,5 11715 95,902613 1,5 11727 95,79491 1,5 7166 95,747126 1,5 7253 95,56314 2 10703 95,379713 2 3102 94,146825 2 11732 93,961076 2 5992 93,93088 2 11833 93,902859 2 7224 93,114144 2 3103 93,022202 2 7164 92,4972 2 12180 91,891892 2 11355 90,585242 2 10679 90,491487 2 7252 89,664613 2 3105 89,112093 2 5200 88,798701 2 5203 86,954782 2 11354 86,686174 2 11357 86,655518 2 10940 86,422977 2 11594 86,189863 2 12179 86,122178 2 7263 84,991274 2 10520 84,532374 2 11733 83,963012 2 7225 83,766234 2 11586 83,194864 2 Catalin-Constantin ROIBU Forestry engineer, PhD 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 +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 https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.htmlhttp://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 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] Script for conditional sums of vectors
Here are some examples of data aggregation functions in R: http://www.slideshare.net/djandrija/data-aggregation-in-r http://www.psychwire.co.uk/2011/04/data-aggregation-in-r-plyr-sqldf-and-data-table/ Andrija On Mon, Feb 4, 2013 at 10:29 AM, Benjamin Gillespie gy...@leeds.ac.ukwrote: Hi guys, I hope you can help me with this (probably) simple query: I have a data frame: -- a=c(1,1,1,1,1,1,2,2,2,2,2,2) b=c(1,1,1,2,3,4,1,1,2,2,3,4) c=c(400,200,300,100,500,300,200,100,500,400,200,100) data=data.frame(a=a,b=b,c=c) -- And I would like to get the following output: -- b a 1 2 3 4 1 900 100 500 300 2 300 900 200 100 -- The values in the output represent the sum of values c in data frame data, for each a and b combination. For example, where a = 1 and b = 1, the output is 400+200+300 = 900. Please would anyone be able to provide a script to create my desired output? Many thanks in advance, Ben Gillespie Research Postgraduate School of Geography University of Leeds Leeds LS2 9JT __ R-help@r-project.org mailing list 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 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] export figure by pdf command
set.seed(2211) x - rnorm(100) #get your working directory getwd() #save it as pdf pdf(hist.pdf) hist(x) dev.off() On Tue, Jan 29, 2013 at 10:22 PM, hp wan huaping@gmail.com wrote: Can you implement it using my provided example? I read the user guide about dev.copy2pdf but I still failed. Thanks 2013/1/30 ilai ke...@math.montana.edu ?dev.copy2pdf On Tue, Jan 29, 2013 at 1:48 PM, hp wan huaping@gmail.com wrote: Dear R mailing listers, After plotting, I wanna save it as file in pdf format using pdf(name.pdf) command. It failed, but I can do it by GUI operation (file-save as-pdf). e.g. x11() hist(x, breaks = 50, probability = FALSE) pdf(hist.pdf) [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.htmlhttp://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 https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.htmlhttp://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 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] apply -- data.frame
hi try with plyr library and function ddply Andrija On 30 Aug 2012 12:58, Sam Steingold s...@gnu.org wrote: Is there a way for an apply-type function to return a data frame? the closest thing I think of is foo - as.data.frame(sapply(...)) names(foo) - c() is there a more elegant way? Thanks! -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://palestinefacts.org http://dhimmi.com http://honestreporting.com http://ffii.org http://mideasttruth.com Lisp: it's here to save your butt. __ R-help@r-project.org mailing list 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 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] apply -- data.frame
I forgot to mention data.table package and also function aggregate as part of base R functions could be useful here Andrija On 30 Aug 2012 13:09, andrija djurovic djandr...@gmail.com wrote: hi try with plyr library and function ddply Andrija On 30 Aug 2012 12:58, Sam Steingold s...@gnu.org wrote: Is there a way for an apply-type function to return a data frame? the closest thing I think of is foo - as.data.frame(sapply(...)) names(foo) - c() is there a more elegant way? Thanks! -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://palestinefacts.org http://dhimmi.com http://honestreporting.com http://ffii.org http://mideasttruth.com Lisp: it's here to save your butt. __ R-help@r-project.org mailing list 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 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] Getting warning message
Hi. I don't know how did you create data frame X but if you check str(X) you will see that you have one or more factors inside. Try using stringsAsFactors=FALSE options while creating data frame. Hope this helps. Andrija On Thu, Jul 26, 2012 at 3:23 PM, namit saileshchowd...@gmail.com wrote: Hi Friends, I have a data frame X, and I want to add % $ in row 4 and 5 respectively. when Im trying using below logic, Im getting warning message. Can anyone help me out on this. X: Summary G Y R T Accts582644 0 1226 AcctCov 230165 0 395 Cov% 4026 0 32 UnCov% 60 74 0 68 EqVol11$MM8.5 10.60 19.1 Using this logic: Z16[5,2:5]-paste($,Z16[5,2:5],sep=) Z16[3,2:5]-paste(Z16[3,2:5],%,sep=) Z16[4,2:5]-paste(Z16[4,2:5],%,sep=) Getting this Warning: In `[-.factor`(`*tmp*`, iseq, value = c(3L, 1L, 2L, 4L, NA)) invalid factor level, NAs generate Final result: Summary G Y R T Accts 582 644 0 1226 AcctCov 230 165 0 395 Cov%40% 26% 0% 32% UnCov% 60% 74% 0% 68% EqVol11$MM$8.50 $10.60 $0 $19.10 Thanks in Advance. Thanks, Namit -- View this message in context: http://r.789695.n4.nabble.com/Getting-warning-message-tp4637928.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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 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] comparing three vectors
Hi. Try this: a=c cb Andrija On Tue, Jul 17, 2012 at 9:02 AM, arunkumar akpbond...@gmail.com wrote: Hi I 've a data a=c(10,20,30) b=c(100,200,300) c=c(50,60,70) I want to compare a[1]=c[1]b[1],.. How to compare for all the records - Thanks in Advance Arun -- View this message in context: http://r.789695.n4.nabble.com/comparing-three-vectors-tp4636728.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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 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] Table/Frame - output
Hi. You should check function addtable2plot from plotrix package. Also, here is an example of modified addtable2plot function that I sometimes use. This function is probably to far from perfect but, maybe, it will give you some ideas how to solve your problem: t2p - function(x=par(usr)[1],y= par(usr)[4], table, cex=par(cex), bg=par(bg), text.col=par(fg)) { plot(1,type=n, axes=F, xlab=, ylab=) column.names-colnames(table) mwidth-strwidth(M,cex=cex) cellwidth - max(strwidth(c(column.names, as.vector(unlist(table))),cex=cex))+mwidth cellheight - max(strheight(c(column.names,as.vector(unlist(table))), cex=cex))*1.5 tabdim-dim(table) xleft - x ytop - y for(row in 1:tabdim[1]) { for(column in 1:tabdim[2]) { if(row==1) par(font=4) else par(font=1) rect(xleft+(column-0.9)*cellwidth, ytop-(row-0.9)*cellheight, xleft+(column)*cellwidth, ytop-row*cellheight, col=bg[row,column],border=NA) text(xleft+(column-0.45)*cellwidth, ytop-(row-0.45)*cellheight, table[row,column],cex=cex,col=text.col) } } } DF - data.frame(x1=1:5, x2=5:1, x3=rep(1,5), x4=rep(5,5)) DF1 - rbind(names(DF), DF) mat - matrix(NA,ncol=ncol(DF), nrow=nrow(DF1)+1) mat[1,] - lightblue mat[seq(2,nrow(mat),2), ] - gray mat[seq(3,nrow(mat),2), ] - white t2p(table=DF1, bg=mat) t2p(table=DF1, bg=mat, x=0.7, y=1.2) As you can see t2p function has some arguments that you can play with in order to get better formatted table. After plotting a table you can save it as bitmap. I hope this helps. Andrija On Tue, Jul 17, 2012 at 9:11 PM, jcrosbie ja...@crosb.ie wrote: I would like to output a nicely formatted data frame to a bitmap. Is this possible in R? -- View this message in context: http://r.789695.n4.nabble.com/Table-Frame-output-tp4636790.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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 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] Subset based on multiple values
Hi. Maybe this: ct - table(test) as.numeric(names(ct[ct==max(ct)])) test[test[,1]%in%as.numeric(names(ct[ct==max(ct)])),,drop=FALSE] ? Andrija On Wed, Jul 11, 2012 at 8:33 PM, Amanduh320 aadam...@uwo.ca wrote: I'm stuck on a seemingly simple problem. I'm trying to subset the data by several numbers and it cuts out half of the rows. Here is the sample code: test - as.matrix(c(1,1,1,1,3,3,7,7,7,7)) Count - tapply(test[,1], test[,1], length) # count for each value spp - unique(test[,1]) Count1 - as.data.frame(cbind(Count,spp)) Max - max(Count) Count1$sppMax - ifelse(Count1$Count = Max, Count1$spp, 0) # only keep values that =Max Count2 - subset(Count1, sppMax 0)#get rid of values that are less than Max AllMax - unique(Count2$sppMax) test2 - subset(test, test[,1] == AllMax) this works where there is only one value for AllMax, how to make it work when there are multiple? Thank you! -- View this message in context: http://r.789695.n4.nabble.com/Subset-based-on-multiple-values-tp4636159.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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 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] convert a table
Hi. Here are some examples that could be useful here: set.seed(1) x - sample(1:2, 100, rep=T) y - sample(1:3, 100, rep=T) TAB - table(x,y) #check the object class class(TAB) apply(TAB, 2, max) apply(TAB[,2:3], 2, max) apply(TAB, 1, max) DF - as.data.frame.matrix(TAB) class(DF) sapply(DF, max) Look at ?class, ?table, ?[ , ?apply, ?as.data.frame.matrix and read posting guide. Andrija On Fri, Jul 6, 2012 at 5:03 PM, Amanduh320 aadam...@uwo.ca wrote: I have my data in a table table - table(test2$Filename, test2$PREDICT) I need to convert this table so it keeps the same structure, but is a different format. The current output is count data by Filename and I want to get the max for each Filename. Columns are: Filename, 1, 2, 3, 4, 5, 6, 7 When I try to convert it to a data.frame it reverts to Var1(Filename), Var2(1:7), Freq. My end goal is to find the max by row (Filename), then do ifelse(xmax, 0, max) for each value in columns 2:8 My problem is that I don't understand what format the table is in. Thank you! Amanda -- View this message in context: http://r.789695.n4.nabble.com/convert-a-table-tp4635615.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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 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] Intersection
Hi. Try with following functions: ?intersection ?%in% ?[ Perhaps someone will provide you more help if you read and follow posting guide http://www.R-project.org/posting-guide.htmlhttp://www.r-project.org/posting-guide.html Andrija On Tue, Jun 26, 2012 at 5:03 PM, ÷ÁÓÉÌØÞÅÎËÏ áÌÅËÓÁÎÄÒ vasilchenko@gmail.com wrote: Hello. I have a problem with 2 dataframes. There are 2 columns - value and dates. These dataframes have different dimension. Some dates coincide. And I need to intersect them by dates and have on output two dataframes with identical columns dates and new dimension . value have to recieve in compliance with dates. Regards, Aleksander. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] flagging values without a loop
Hi. Yes it is possible. Here is one approach: DF - read.table(textConnection( Unit DayHour Price Flag afd11/2/20031 1 N afd11/2/20031 2 N afd11/2/20031 3 N afd11/2/20031 4 Y dcf11/2/20032 2 N dcf11/2/20032 3 Y dcf11/2/20032 1 N dcf11/2/20032 2 N dcf11/2/20032 3 Y ghg21/2/20033 1 N afd11/2/20033 2 N ),header=TRUE) cbind(DF, flag = ave(DF$Price, DF$Hour, FUN=function(x) ifelse(x==max(x), 1, 0))) Unit Day Hour Price Flag flag 1 afd1 1/2/20031 1N0 2 afd1 1/2/20031 2N0 3 afd1 1/2/20031 3N0 4 afd1 1/2/20031 4Y1 5 dcf1 1/2/20032 2N0 6 dcf1 1/2/20032 3Y1 7 dcf1 1/2/20032 1N0 8 dcf1 1/2/20032 2N0 9 dcf1 1/2/20032 3Y1 10 ghg2 1/2/20033 1N0 11 afd1 1/2/20033 2N1 On Thu, Jun 7, 2012 at 4:17 PM, jcrosbie ja...@crosb.ie wrote: For a given hour I want to be able to add a new column called flag. The flag column will flag the highest price in a given hour. Is there a way to do this without a loop? matrix: Unit, Day,Hour, Price, Flag afd11/2/20031 1 N afd11/2/20031 2 N afd11/2/20031 3 N afd11/2/20031 4 Y dcf11/2/20032 2 N dcf11/2/20032 3 Y dcf11/2/20032 1 N dcf11/2/20032 2 N dcf11/2/20032 3 Y ghg21/2/20033 1 N afd11/2/20033 2 N . -- View this message in context: http://r.789695.n4.nabble.com/flagging-values-without-a-loop-tp4632689.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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 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] Removing Double Quotations After Using Cbind
Hi. Try: as.data.frame(cbind(a[,1],a[,2])) Andrija On Wed, Jun 6, 2012 at 9:51 PM, Joshua Budman josh.bud...@gmail.com wrote: Hi, I am trying to process genomics data and the presence of both characters and integers in an array is giving issues. The following is an example: a-array(c(2,2,X,1:3,2:4),dim=c(3,3)) b-cbind(a[,1],a[,2]) With the output being: [,1] [,2] [1,] 2 1 [2,] 2 2 [3,] X 3 Is there any way for me to remove the quotation marks from every integer/character in the new array? Or, is there a way to create the new array without getting the quotation marks? Regards, Josh [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] how to remove part of the string
Hi. You can do something like this: gsub(\\|\\|Leukotriene A4 hydrolase,,LTA4H||Leukotriene A4 hydrolase) Andrija On Wed, Jun 6, 2012 at 10:45 PM, Bill Hyman billhym...@yahoo.com wrote: Dear all, Does any one know how to remove part of the string? For example, LTA4H||Leukotriene A4 hydrolase is a gene name plus gene description. I hope to remove ||Leukotriene A4 hydrolase. What would be the R code to do that using gsub()? Many thanks! Bill __ R-help@r-project.org mailing list 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 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] how to remove part of the string
Hi. Rui already gave you a solution. Beside that you can, also, use substr function in this concrete example: substr(LTA4H||Leukotriene A4 hydrolase, 1, 5) This can be adjusted to rest of your data also, but you haven't provided enough information. Andrija On Wed, Jun 6, 2012 at 11:27 PM, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, Try txt - LTA4H||Leukotriene A4 hydrolase pattern - \\|\\|.*$ gsub(pattern, , txt) Hope this helps, Rui Barradas Em 06-06-2012 21:45, Bill Hyman escreveu: Dear all, Does any one know how to remove part of the string? For example, LTA4H||Leukotriene A4 hydrolase is a gene name plus gene description. I hope to remove ||Leukotriene A4 hydrolase. What would be the R code to do that using gsub()? Many thanks! Bill __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guidehttp://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html 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 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] Rolling Sample VAR
Hi. rollapply function for zoo package could be a useful here. library(zoo) ?rollapply Andrija On Fri, May 25, 2012 at 5:22 PM, bantex bantexmutat...@hotmail.com wrote: hi guys, I am using trivariate VAR model to get 10 step ahead orthogonalized impulse response functions. I want to use rolling sample analysis on the coefficients of the irf but I have no idea how to do that. I looked through the forums but I can't seem to find any solutions. Any suggestions would be helpful. B -- View this message in context: http://r.789695.n4.nabble.com/Rolling-Sample-VAR-tp4631328.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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 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] count number of groups
Hi. try using table function: ID=c(1,1,1,1,2,2,2,2,3,3,3,3) table(IF) ID 1 2 3 4 4 4 Also check ?tapply function Andrija On Fri, May 25, 2012 at 5:38 PM, Charles Determan Jr deter...@umn.eduwrote: Hello, Simple question that I am stuck on and can't seem to find an answer in the help files currently. I have a list which contains repeated ID's. I would like to have R count the number of ID's. For example: ID=c(1,1,1,1,2,2,2,2,3,3,3,3) as.data.frame(ID) Clearly, there are 3 groups. How would I have R give me the summary: ID 3 Many thanks, Charles [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] odbcConnectExcel() fails to fetch all columns
Hi. I use RODBC for importing Excel files quiet often and never got the similar problem. Have you tried with sqlQuery? z - odbcConnectExcel(./BBaselinePtQaires_apr2011.xls) BQ - sqlQuery(z, select * from [BBaselinePtQaires$]) Andrija On Fri, Apr 20, 2012 at 11:57 PM, Jeff Newmiller jdnew...@dcn.davis.ca.us wrote: Excel is not a database, and the Excel ODBC driver is extremely limited. Put your data in a CSV file or a SQL database (even a Jet database is a step up from Excel). http://www.stata.com/support/faqs/data/odbc_excel.html --- Jeff Newmiller The . . Go Live... DCN:jdnew...@dcn.davis.ca.us Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. Andrew Roberts and...@thinkingbone.org wrote: Folks, Is there a parameter somewhere in RODBC that enables more columns to be retrieved from an Excel worksheet? # This next bit uses an undocumented call in RODBC z - odbcConnectExcel(./BBaselinePtQaires_apr2011.xls) BQ - sqlFetch(z, BBaselinePtQaires) Gives me: z RODBC[1] And BQ 134 obs. of 59 variables I have all the rows in the worksheet but only the first 59 out of a total of 70 columns. I’m in RStudio 0.95.263 using RODBC 1.3-3 and R version 2.12.2 (2011-02-25). I'm puzzled - the worksheet seems ok. If the worst comes to the worst I will have to split the worksheet and cbind to put it back together but that seems inelegant. The worksheet contains 134 rows, 70 columns and is in a spreadsheet that weighs in at 154 KB in total. Can you help unbaffle me? Andrew __ R-help@r-project.org mailing list 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 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 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] Database connectivity
RODBC On Mon, Apr 16, 2012 at 11:40 AM, Partha Sinha pnsinh...@gmail.com wrote: Dear All please let me know how to connect SQL server from R? which all packages I will require for this? Thanks Parth __ R-help@r-project.org mailing list 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 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] Error: R for Windows GUI front-end has stopped working
Hi all. I found one situation, on my OS - Windows 7, where R stops working with reported error R for Windows GUI front-end has stopped working. Here is the example: library(plyr) DF - data.frame(x=c(1:3, NA, NA), y=factor(sample(1:3,5,rep=T),levels=1:5)) DF[DF$x3, ] #this works properly ddply(DF, .(y), nrow, .drop=FALSE) #this causes the problem ddply(DF[DF$x3, ], .(y), nrow, .drop=FALSE) Sometimes R deals with this without closing a program and with reported error: Error in split_indices(seq_along(splitv), as.integer(splitv), attr(splitv, : INTEGER() can only be applied to a 'integer', not a 'char' but in the most of cases just stops working. Does anyone know if this is happening also with other OS or only with Windows 7. Here is my session info: sessionInfo() R version 2.15.0 (2012-03-30) Platform: i386-pc-mingw32/i386 (32-bit) locale: [1] LC_COLLATE=English_United States.1252 [2] LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] plyr_1.7.1 Andrija __ R-help@r-project.org mailing list 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] Error: R for Windows GUI front-end has stopped working
Peter, thanks for clarification. Andrija On Sat, Apr 14, 2012 at 12:11 PM, peter dalgaard pda...@gmail.com wrote: Yes, that causes a segmentation fault on OSX too -- ddply(DF[DF$x3, ], .(y), nrow, .drop=FALSE) *** caught segfault *** address 0x0, cause 'memory not mapped' Traceback: 1: .Call(split_indices, index, group, as.integer(n)) 2: split_indices(seq_along(splitv), as.integer(splitv), attr(splitv, n)) 3: splitter_d(.data, .variables, drop = .drop) 4: ddply(DF[DF$x 3, ], .(y), nrow, .drop = FALSE) Possible actions: 1: abort (with core dump, if enabled) 2: normal R exit 3: exit R without saving workspace 4: exit R saving workspace The problem is in a C function from the plyr package, so you need to take it up the maintainer. -pd On Apr 14, 2012, at 11:28 , andrija djurovic wrote: Hi all. I found one situation, on my OS - Windows 7, where R stops working with reported error R for Windows GUI front-end has stopped working. Here is the example: library(plyr) DF - data.frame(x=c(1:3, NA, NA), y=factor(sample(1:3,5,rep=T),levels=1:5)) DF[DF$x3, ] #this works properly ddply(DF, .(y), nrow, .drop=FALSE) #this causes the problem ddply(DF[DF$x3, ], .(y), nrow, .drop=FALSE) Sometimes R deals with this without closing a program and with reported error: Error in split_indices(seq_along(splitv), as.integer(splitv), attr(splitv, : INTEGER() can only be applied to a 'integer', not a 'char' but in the most of cases just stops working. Does anyone know if this is happening also with other OS or only with Windows 7. Here is my session info: sessionInfo() R version 2.15.0 (2012-03-30) Platform: i386-pc-mingw32/i386 (32-bit) locale: [1] LC_COLLATE=English_United States.1252 [2] LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] plyr_1.7.1 Andrija __ R-help@r-project.org mailing list 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. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.com __ R-help@r-project.org mailing list 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] R equivalent for SQL query
Hi, here are some solutions: DF - read.table(textConnection( A B C 1 1 3 1 1 4 1 1 5 1 2 6 1 2 7 1 3 8 ), header=TRUE) #using sqldf package library(sqldf) sqldf(select A, B, count(*) from DF group by A, B order by count(*) desc) #using function table as.data.frame(table(DF$A, DF$B)) As you can see, you can use sqldf package for performing sql queries on R data frames. Andrija On Tue, Apr 3, 2012 at 8:26 PM, Steven Raemaekers s.raemaek...@sig.eu wrote: Hi, I have a query which I would like to translate into R, but I do not know how to do it in an easy way. Assume a data frame has columns A, B and C: A B C 1 1 3 1 1 4 1 1 5 1 2 6 1 2 7 1 3 8 The query is as follows: select A, B, count(*) from data.frame group by A, B order by count(*) desc How do I translate this into R statements in such way that the result is a data frame structured as follows: A B count(*) 1 1 3 1 2 2 1 3 1 Thanks, Steven __ R-help@r-project.org mailing list 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 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] subset problem
Hi. You can try this: df[type%in%type_list, ] You can also use sqldf package and subset data frames usign sql statements: library(sqldf) df - data.frame(x1=1:10, type=10:1) type_list - data.frame(index=seq(1,10,by=2)) sqldf(select df.* from df where df.type in (select * from type_list)) Andrija On Thu, Mar 29, 2012 at 7:25 AM, reeyarn reey...@gmail.com wrote: Hi, If my data frame df has a index type, I want to get a subset such that the type belongs to a type_list; using sql, I want SELECT name, type FROM df WHERE type in type_list; Now in R I have to write a loop like mysubset- df [ df$type == type_list[1], ] for (type1 in type_list[ 2: length (type_list) ] ) { mysubset-cbind (mysubset, df [ df$type == type1, ]) } What is the natural way of doing this in R? Is it possible to use subset() to attain this? Thanks! Best, Reeyarn On Fri, Dec 3, 2010 at 11:26 AM, William Dunlap [hidden email] wrote: HI, I have a dataframe like this: name type A t1 B t2 C t1 D t4 E t3 F t2 how can I have a sub dataframe based with the column type like this: (for type = t1) name type A t1 C t1 D t1 Hi, Let's say your data.frame is stored in a variable named df: R subset(df, type == 't1') Read the help files: R ?subset Also take a look at ?split -steve __ R-help@r-project.org mailing list 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 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] function for filtering and deleting vector entries
Hi. Maybe this will help you: set.seed(1) temp - 1:100 v - rnorm(100) temp[temp16 | temp38] v[temp16 | temp38] Andrija On Wed, Feb 29, 2012 at 7:09 PM, babyluck madr...@gmx.ch wrote: Dear helpers I have two data sets saved as vectors (temperature and velocity). Now I need to take out a span of temperature and its corresponding velocity in the other vector. How can I achieve that? I tried to write a function,which takes a vector entry and then decides wether to delete the temperature entry or not and simultaneously doing so with same entry in the velocity vector.. But somehow it's not working...could somebody please help me? Thanks a lot.. norm = function(Temp,v){ for (i in 1:length(Temp)){ if (Temp[i]=16 || Temp[i] = 38.5) {Temp[-i];v[-i]} return(Temp,v) } } -- View this message in context: http://r.789695.n4.nabble.com/function-for-filtering-and-deleting-vector-entries-tp4432410p4432410.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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 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] How to replace the values in a column
Hi. You can do something like this: df - read.table(textConnection( GenRep A_1 1 A_1 2 A_2 1 A_2 2 B_1 1 B_1 2 B_3 1 B_3 2 OP1_1 1 OP1_1 2 OP1_5 1 OP1_5 2),header=TRUE,stringsAsFactors=FALSE) str(df) #adding a new column cbind(df,ncol = ifelse(df$Gen==A_1,Wynda,ifelse(df$Gen==B_3,Wynda,df$Gen))) #replacing exisiting values df$Gen[df$Gen==A_1 | df$Gen==B_3] - Wynda df Andrija On Wed, Feb 29, 2012 at 7:06 AM, ilai ke...@math.montana.edu wrote: Hannah, If Gen is a factor you can simply build the new factor on top of it: dataframe$Gen- factor( c('Wynda' , 'A_2' , 'B_1' , 'Wynda' , 'Wynda' , 'OP1_5')[Gen] ) Just make sure the replacement labels are in the same order as levels(Gen). Cheers On Tue, Feb 28, 2012 at 8:39 PM, hannahmaohuang hannahmaohu...@gmail.com wrote: Dear All, I've been searching relevant topics about replacing values, none seemed to be applicable to me... I have a file with many many varieties, and want to replace some of them into different names. I tried various of ways, still don't know how to do that most efficiently.. Here is part of the example data: Gen Rep A_1 1 A_1 2 A_2 1 A_2 2 B_1 1 B_1 2 B_3 1 B_3 2 OP1_1 1 OP1_1 2 OP1_5 1 OP1_5 2 For example, I want to replace A_1, B_3, OP1_1 into different name Wynda So that the expected file should become: Gen Rep Wynda 1 Wynda 2 A_2 1 A_2 2 B_1 1 B_1 2 Wynda 1 Wynda 2 Wynda 1 Wynda 2 OP1_5 1 OP1_5 2 I have created a link file, which contains two rows, translating which Gen correlating to which Name. Not sure if this file helps or not, example as below: Column1(Gen) Column2(Name) A_1 Wynda A_2 A_2 B_1 B_1 B_3 Wynda OP1_1 Wynda OP1_5 OP1_5 Though I can replace one by one in excel, since there are too many files and too many reps, it'll be very time-consuming also easy to make mistakes. Please give me any guidance or help in terms of finish this with R. Thanks so much ! Sincerely Hannah -- View this message in context: http://r.789695.n4.nabble.com/How-to-replace-the-values-in-a-column-tp4430448p4430448.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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 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 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] Making a point-sampling matrix
Hi. If I understood you correctly here is one approach to your solution: k - 5 mat - diag(1,nrow=k,ncol=k) set.seed(10) samp - mat[sample(1:k,3,rep=FALSE),] samp [,1] [,2] [,3] [,4] [,5] [1,]00100 [2,]01000 [3,]00010 #check apply(samp,1,sum) [1] 1 1 1 apply(samp,2,sum) [1] 0 1 1 1 0 I hope this helps Andrija On Sat, Feb 4, 2012 at 4:27 PM, Alaios ala...@yahoo.com wrote: Dear all, I would like to create a k,n matrix which that will include - k random elements with value 1 -all rest zeros -one single 1 in each row - each column will have maximum one 1 so far I have a so stupid function that with nested while tries to fit k numbers randomly in a row by row fashion, checking also if the c column rule is violated. In that case there is also another loop that will try to find a new solution. That way it takes ages, as it is natural to complete a matrix of 300,512 as the new ones conflict all the time. I am trying to find an easy way to make this algorithm way simpler that will help me reduce the time needed. I was thinking then if R can help me by shuffling number from 1 to 300 randomly, so to avoid the first loop that does that goes row by row.. and then find an easy way to check if the column rule is violated. Do you think that you can spend some time help me? Have a nice weekend Regards Alex [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] mode of frequency distribution table
Hi. You can do something like this: #find the most frequent values of x t - table(x) t[t==max(t)] 5 8 #sort table t based on frequencies t[order(as.numeric(t),decreasing = TRUE)] x 5 6 4 17 1 2 13 30 100 300 8 5 4 2 1 1 1 1 1 1 #extract any range from sorted table t[order(as.numeric(t),decreasing = TRUE)][1:3] x 5 6 4 8 5 4 I hope this helps. Andrija On Sun, Jan 8, 2012 at 1:48 PM, Mary Kindall mary.kind...@gmail.com wrote: In a frequency distribution table (bell shaped), how can we find the most frequent range? for example: x = c(1,2, 4,4,4,4, 5,5,5,6,6,5,5,5,5,5,6,6,6,13, 17,17,30,100,300) barplot(table(x)) In the code above, which function do we use to find that the most frequent value range from 4 to 6. Thanks. -- - Mary Kindall Yorktown Heights, NY USA [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] Combining characters
Hi. You can use expand.grid here expand.grid(x,y,z) Andrija On Wed, Jan 4, 2012 at 5:32 PM, jeremy jeremynamer...@gmail.com wrote: Hi all, I'm trying to combine exhaustively several character arrays in R like: x=c(one,two,three) y=c(yellow,blue,green) z=c(apple,cheese) in order to get concatenation of x[1] y[1] z[1] (one yellow apple) x[1] y[1] z[2] (one yellow cheese) x[1] y[2] z[1](one blue apple) ... x[length(x)] y[length(y)] z[length(z)] (three green cheese) Anyone has a solution ? Thank in advance -- View this message in context: http://r.789695.n4.nabble.com/Combining-characters-tp4261888p4261888.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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 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] Creating and assigning variable names in loop
Hi. You assign two times different values to variable label. Try this and i hope you will notice a mistake: i=1 label - paste(score, i, sep=_) label assign(label, x1+(x2*i) ) label Instead of this you can do something like: rm(list=ls()) n = 10 set.seed(1) x1 = rnorm(n,0) x2 = rnorm(n,0) samp_data - data.frame(x1,x2) for( i in 1:3) { samp_data - cbind(samp_data, x1+(x2*i)) colnames(samp_data)[i+2] - paste(score, i, sep=_) } head(samp_data) Andrija On Wed, Dec 21, 2011 at 11:01 AM, aajit75 aaji...@yahoo.co.in wrote: Hello List I am trying to create and assign variable names in loop, but not able to get expected variable names. Here is the sample code n = 10 set.seed(1) x1 = rnorm(n,0) x2 = rnorm(n,0) samp_data - data.frame(x1,x2) for( i in 1:3) { label - paste(score, i, sep=_) assign(label, value =x1+(x2*i) ) samp_data - cbind(samp_data, get(label)) } head(samp_data) x1 x2 get(label) get(label) get(label) 1 -0.6264538 1.51178117 0.8853274 2.3971085 3.9088897 2 0.1836433 0.38984324 0.5734866 0.9633298 1.3531730 3 -0.8356286 -0.62124058 -1.4568692 -2.0781098 -2.6993504 4 1.5952808 -2.21469989 -0.6194191 -2.8341190 -5.0488189 5 0.3295078 1.12493092 1.4544387 2.5793696 3.7043005 6 -0.8204684 -0.04493361 -0.8654020 -0.9103356 -0.9552692 I am expecting new variables to be created in the samp_data are score_1 score_2 score_3, instead get(label) get(label) get(label) Where am I going wrong? Thanks in advance ~Ajit -- View this message in context: http://r.789695.n4.nabble.com/Creating-and-assigning-variable-names-in-loop-tp4221080p4221080.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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 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] matching using which
Hi. Here is one approach: if (length(b)0) data[,-b] else data Andrija On Thu, Dec 8, 2011 at 1:25 PM, Vikram Bahure economics.vik...@gmail.comwrote: Dear R users, I have a very simple query. I am using the following command, which should give me row no. for the matching colnames. It works well for matching the colnames but if there is no column matching it gives me outcome as integer(0) which I am not able to use in further calculation. It would be very helpful to have some insight. * * * b - which(colnames(data)%in%c(X.,X))* * b* *integer(0)* * b0* *logical(0)* *b- data[,-b] # does not work properly* * * Regards Vikram [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] sum of deviations from the weighted mean
Hi all. I tried to calculate sum of deviations from the weighted mean and i didn't get what i expected - 0. Here is an example: wt - c(10,25,38,22,5) x - 6:10 wm - weighted.mean(x,wt) (x-wm)*wt [1] -18.70 -21.75 4.94 24.86 10.65 sum((x-wm)*wt) [1] -1.24345e-14 With simple mean I got 0: sum(x-mean(x)) [1] 0 Could someone explain me why we didn't get 0, with weighted mean, as it is expected? Thanks in advance Andrija [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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] sum of deviations from the weighted mean
Thank and sorry i should check this before asking the question. Andrija On Thu, Dec 8, 2011 at 3:20 PM, R. Michael Weylandt michael.weyla...@gmail.com michael.weyla...@gmail.com wrote: R FAQ 7.31 I'd imagine. (Floating point arithmetic and limitations thereof) Michael On Dec 8, 2011, at 9:15 AM, andrija djurovic djandr...@gmail.com wrote: Hi all. I tried to calculate sum of deviations from the weighted mean and i didn't get what i expected - 0. Here is an example: wt - c(10,25,38,22,5) x - 6:10 wm - weighted.mean(x,wt) (x-wm)*wt [1] -18.70 -21.75 4.94 24.86 10.65 sum((x-wm)*wt) [1] -1.24345e-14 With simple mean I got 0: sum(x-mean(x)) [1] 0 Could someone explain me why we didn't get 0, with weighted mean, as it is expected? Thanks in advance Andrija [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] Paste() - Get all possible combinations from multiple vectors
Hi. Maybe this will help you: expand.grid(x,y,z) apply(expand.grid(x,y,z),1, function (x) paste(x[1], x[2], x[3], sep=)) Andrija On Tue, Dec 6, 2011 at 1:53 PM, Gaj Stan (BIGCAT) stan@maastrichtuniversity.nl wrote: Hello fellow R-users, Given are three vectors and the outcome would be all possible combinations in combination with the paste() function. For example: x - c(1:3) y - letters[1:3] z - LETTERS[1:3] My result would thus be 18 names based on all possible combinations between these vectors: 1 a A 1 a B, 1 a C, 1 b A, 1 b B, 1 b C, 1 c A, 1 c B, 1 c C, etc. To solve the issue above I would use: paste(rep(x, each=9), rep(y, each=6), z) This is a very straightforward example, but when I have vectors of different sizes, it will be much more difficult (for me) to create this. Is there an easier way to do this? Many thanks in advance, -- Stan [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] RODBC connect to Excel (64-bit Windows 7)
Hi to all. I have a problem to connect to an Excel database using RODBC. Namely, I am using 64-bit R 2.14.0, under Windows 7 and I tried following: library(RODBC) channel - odbcConnectExcel(results.xlsx) Error in odbcConnectExcel(results.xlsx) : odbcConnectExcel is only usable with 32-bit Windows # ok this is clear why it doesn't work channel - odbcConnectExcel2007(results.xlsx) # this was one of proposals from old R help posts, but it doesn't work for me Warning messages: 1: In odbcDriverConnect(con, tabQuote = c([, ]), ...) : [RODBC] ERROR: state IM002, code 0, message [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified 2: In odbcDriverConnect(con, tabQuote = c([, ]), ...) : ODBC connection failed After that as it suggested in R data import/export manual I installed AccessDatabaseEngine.exe. Also I used ODBC in c:\Windows\SysWOW64\odbcad32 to create data source called result. I check if it is created: odbcDataSources() dBASE Files Microsoft Access dBASE Driver (*.dbf, *.ndx, *.mdx) MS Access Database Microsoft Access Driver (*.mdb, *.accdb) Excel file Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb) * results* Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb) and tried again but it doesn't work: channel - odbcDriverConnect() Warning messages: 1: In odbcDriverConnect() : [RODBC] ERROR: state IM014, code 0, message [Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application 2: In odbcDriverConnect() : ODBC connection failed Could someone guide me what should I try next to fix a problem? Is this some problem with Drivers or not? Thanks in advance Andrija [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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] RODBC connect to Excel (64-bit Windows 7)
Hi. Not specifically, but I used it before with 32-bit R under Windows XP and it worked just fine so i though to keep using it.Anyway I will lookt at XLConnect. Thank you for suggestion. Andrija On Sun, Dec 4, 2011 at 7:41 PM, R. Michael Weylandt michael.weyla...@gmail.com michael.weyla...@gmail.com wrote: Do you need to use RODBC specifically? I've been using XLConnect quite a lot recently and have been quite pleased with it. Michael On Dec 4, 2011, at 9:40 AM, andrija djurovic djandr...@gmail.com wrote: Hi to all. I have a problem to connect to an Excel database using RODBC. Namely, I am using 64-bit R 2.14.0, under Windows 7 and I tried following: library(RODBC) channel - odbcConnectExcel(results.xlsx) Error in odbcConnectExcel(results.xlsx) : odbcConnectExcel is only usable with 32-bit Windows # ok this is clear why it doesn't work channel - odbcConnectExcel2007(results.xlsx) # this was one of proposals from old R help posts, but it doesn't work for me Warning messages: 1: In odbcDriverConnect(con, tabQuote = c([, ]), ...) : [RODBC] ERROR: state IM002, code 0, message [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified 2: In odbcDriverConnect(con, tabQuote = c([, ]), ...) : ODBC connection failed After that as it suggested in R data import/export manual I installed AccessDatabaseEngine.exe. Also I used ODBC in c:\Windows\SysWOW64\odbcad32 to create data source called result. I check if it is created: odbcDataSources() dBASE Files Microsoft Access dBASE Driver (*.dbf, *.ndx, *.mdx) MS Access Database Microsoft Access Driver (*.mdb, *.accdb) Excel file Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb) * results* Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb) and tried again but it doesn't work: channel - odbcDriverConnect() Warning messages: 1: In odbcDriverConnect() : [RODBC] ERROR: state IM014, code 0, message [Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application 2: In odbcDriverConnect() : ODBC connection failed Could someone guide me what should I try next to fix a problem? Is this some problem with Drivers or not? Thanks in advance Andrija [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] equating approximate values
Hi. Maybe this can help you (you can try additionally to incorporate threshold): set.seed(1) x-rnorm(10,10,1) values - sample(1:10,10) #values that we are looking for mat - matrix(c(x,values),ncol=2) closest-function(x,values)#function is an example from The R book (Crawley) + { + x[which(abs(x-values)==min(abs(x-values)))] + } apply(mat[,2,drop=FALSE],1,function(y) closest(mat[,1],y)) [1] 10.183643 9.164371 9.164371 9.164371 9.164371 9.164371 9.164371 9.164371 [9] 9.164371 9.164371 On Sun, Dec 4, 2011 at 7:00 PM, vamshi999 vamshi...@gmail.com wrote: Hello List, I am having trouble finding the command for my problem. I have two arrays x and y. now i would like to compare the values of x and y and then get the index of x which is exactly or approximately equal(+/- some value ) to the values in y. x - runif(100,min=0,max=5) y - runif(10,min=0,max=5) the threshold value(+/-) value can vary. for this example lets take it to be .5 I know the regular method of doing this by writing different if and for loops. But i have very big dataframe the computation time is very high for this method. can anyone please tell me if there any functions to do this. thank you for your help. -- View this message in context: http://r.789695.n4.nabble.com/equating-approximate-values-tp4157551p4157551.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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 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] Iteration in R
Hi. One approach is using replicate. See ?replicate: replicate(3,rnorm(100,1,2)) Andrija On Sat, Dec 3, 2011 at 7:10 PM, Martin Zonyrah martin20...@yahoo.comwrote: Hi, I need help. I am trying to iterate this command x - rnorm(100, 1.0, 2.0) one hundred times in R but I don't seem to have a clue. Can anyone help? Your help is very much appreciated. Martin [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] Iteration in R
Hi Brad. Maybe something like this: lapply(rep(1,6), function(x) rnorm(10,0,1)) Andrija On Sat, Dec 3, 2011 at 8:21 PM, B77S bps0...@auburn.edu wrote: Hi Michael, How would you do this with lapply to return a list? I can't seem to get that to work (I haven't used these much and am trying to learn). Thanks Brad Michael Weylandt wrote ? replicate or a for loop or do all one hundred simulations at once x - matrix(rnorm(100^2, 1, 2), 100) It's going to depend on what you want to do with the numbers. Michael On Sat, Dec 3, 2011 at 1:10 PM, Martin Zonyrah lt;martin2005z@gt; wrote: Hi, I need help. I am trying to iterate this command x - rnorm(100, 1.0, 2.0) one hundred times in R but I don't seem to have a clue. Can anyone help? Your help is very much appreciated. Martin [[alternative HTML version deleted]] __ R-help@ mailing list 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@ mailing list 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. -- View this message in context: http://r.789695.n4.nabble.com/Iteration-in-R-tp4154433p4154479.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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 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] Transforming a string into a command
?eval s- expression(log(a+b)) a-1 b-2 eval(s) Andrija On Sun, Nov 27, 2011 at 11:16 AM, Victor vdem...@gmail.com wrote: I would like to make a string executable, e.g, s- ln(a+b) a-1 b-2 execute string s to obtain ln(a+b) How can I make it? Ciao fron Rome Vittorio __ R-help@r-project.org mailing list 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 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] Simplifying my code
Hi. You haven't specified object to store results. Try something like: dat = c() #or dat = list() or matrix with specified ncol and nrow for(i in 1:20){ dat[i]=complete(dat.mice,[i] } On Sun, Nov 27, 2011 at 4:02 PM, Christopher Desjardins desja...@umn.eduwrote: Hi, I have a pretty simple problem. Here is the code: dat1=complete(dat.mice,1) dat2=complete(dat.mice,2) dat3=complete(dat.mice,3) dat4=complete(dat.mice,4) dat5=complete(dat.mice,5) dat6=complete(dat.mice,6) dat7=complete(dat.mice,7) dat8=complete(dat.mice,8) dat9=complete(dat.mice,9) dat10=complete(dat.mice,10) dat11=complete(dat.mice,11) dat12=complete(dat.mice,12) dat13=complete(dat.mice,13) dat14=complete(dat.mice,14) dat15=complete(dat.mice,15) dat16=complete(dat.mice,16) dat17=complete(dat.mice,17) dat18=complete(dat.mice,18) dat19=complete(dat.mice,19) dat20=complete(dat.mice,20) I would like to simplify this into a for loop. I thought this would work: for(i in 1:20){ dat[i]=complete(dat.mice,[i] } But it doesn't. Any tips? Chris [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] Is there way to add a new row to a data frame in a specific location
Hi. May be this: df = data.frame( A=c('a','b','c'), B=c(1,2,3), C=c(10,20,30), stringsAsFactors=FALSE) newrow = c('X', 100, 200) rbind(df,newrow)[c(1,4,2,3),] Andrija On Thu, Nov 24, 2011 at. 6:05 PM, Sammy Zee szee2...@gmail.com wrote: Is there easy way (without copying the existing rows to a temporary location and copying back) to add a new row to a specific index location in an existing data frame? Example df = data.frame( A= c('a','b','c'), B=c(1,2,3), C=(10,20,30)) newrow = c('X', 100, 200) I want to add the newrow as the second row to the data frame df Please suggest a solution that is efficient for a data frame that can have millions of rows, and I want to add a new row in any given index location of the data frame. Thanks, Sammy [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] Correlation Matrix in R
Hi, one solution is to use sink. Check ?sink to see explanation and following example. sink(sink-examp.txt) i - 1:10 outer(i, i, *) sink() Andrija On Tue, Nov 1, 2011 at 10:43 AM, AlexC alexandre.chaus...@unil.ch wrote: Hello, Thank you for your replies. I cannot run the function rcor.test even when having loaded package ltm. Perhaps it has to do with the fact that I am using the latest version of R and this package wasn't created under that version The function corr.test in package psych works fine. Is there anyway to export the results in a txt or csv file? Since it isn't in a data frame format it cannot simply be exported using write.table Alexandre -- View this message in context: http://r.789695.n4.nabble.com/Correlation-Matrix-in-R-tp3938274p3962939.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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 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] How to get Quartiles when data contains both numeric variables and factors
Hi, you are almost there: sapply(x, function(x) quantile(as.numeric(x), c(0.01, 0.99))) x1 x2 x3x4 x5 x6 1% 0.0351777 0.007628441 0.225533 0.4459064 1 1 99% 0.9938919 0.964901423 1.826894 3.6226944 3 2 Andrija On Mon, Oct 31, 2011 at 2:09 PM, aajit75 aaji...@yahoo.co.in wrote: When data contains both factor and numeric variables, how to get quartiles for all numeric variables? n - 100 x1 - runif(n) x2 - runif(n) x3 - x1 + x2 + runif(n)/10 x4 - x1 + x2 + x3 + runif(n)/10 x5 - factor(sample(c('a','b','c'),n,replace=TRUE)) x6 - factor(1*(x5=='a' | x5=='c')) data1 - cbind(x1,x2,x3,x4,x5,x6) data - data.frame(data1) data - within(data,{x5 - factor(x5)}) x - data qs - sapply(x, function(x) quantile(x, c(0.01, 0.99))) I get an error: Error in quantile.default(x, c(min_pct, max_pct)) : factors are not allowed Thanks for the help. -- View this message in context: http://r.789695.n4.nabble.com/How-to-get-Quartiles-when-data-contains-both-numeric-variables-and-factors-tp3955750p3955750.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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 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] How to get Quartiles when data contains both numeric variables and factors
Or this: str(x) 'data.frame': 100 obs. of 6 variables: $ x1: num 0.4548 0.0352 0.6353 0.6017 0.8588 ... $ x2: num 0.849 0.335 0.986 0.617 0.212 ... $ x3: num 1.35 0.46 1.67 1.23 1.14 ... $ x4: num 2.67 0.91 3.31 2.48 2.28 ... $ x5: Factor w/ 3 levels 1,2,3: 3 1 3 3 3 1 2 3 3 1 ... $ x6: num 2 2 2 2 2 2 1 2 2 2 ... sapply(x[,sapply(x,is.numeric)], function(x) quantile(as.numeric(x), c(0.01, 0.99))) x1 x2 x3x4 x6 1% 0.0351777 0.007628441 0.225533 0.4459064 1 99% 0.9938919 0.964901423 1.826894 3.6226944 2 Hope this helps. Andrija On Mon, Oct 31, 2011 at 2:09 PM, aajit75 aaji...@yahoo.co.in wrote: When data contains both factor and numeric variables, how to get quartiles for all numeric variables? n - 100 x1 - runif(n) x2 - runif(n) x3 - x1 + x2 + runif(n)/10 x4 - x1 + x2 + x3 + runif(n)/10 x5 - factor(sample(c('a','b','c'),n,replace=TRUE)) x6 - factor(1*(x5=='a' | x5=='c')) data1 - cbind(x1,x2,x3,x4,x5,x6) data - data.frame(data1) data - within(data,{x5 - factor(x5)}) x - data qs - sapply(x, function(x) quantile(x, c(0.01, 0.99))) I get an error: Error in quantile.default(x, c(min_pct, max_pct)) : factors are not allowed Thanks for the help. -- View this message in context: http://r.789695.n4.nabble.com/How-to-get-Quartiles-when-data-contains-both-numeric-variables-and-factors-tp3955750p3955750.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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 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] why the a[-indx] does not work?
as.logical(c(1,0,1,1)) [1] TRUE FALSE TRUE TRUE ?as.logical On Sun, Oct 30, 2011 at 8:50 PM, Alaios ala...@yahoo.com wrote: probably you mean For [-indexing only: i, j, ... can be logical vectors, indicating elements/slices to select. Such vectors are recycled if necessary to match the corresponding extent. i, j, ... can also be negative integers, indicating elements/slices to leave out of the selection. How can i convert the positives to TRUE and zeros and FALSE? From: William Dunlap wdun...@tibco.com Sent: Sunday, October 30, 2011 9:17 PM Subject: RE: [R] why the a[-indx] does not work? a[overLoadTesT==0] [1] 2 4 5 6 7 8 9 10 Look into help('[') or help('Subscript') to see how integer and logical (Boolean) subscripts differ. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Alaios Sent: Sunday, October 30, 2011 11:52 AM To: R-help@r-project.org Subject: [R] why the a[-indx] does not work? Dear all, Could you please explain me why OverloadsTesT [1] 1 0 1 0 0 0 0 0 0 0 a-matrix(data=seq(1,10),nrow=10) a [,1] [1,]1 [2,]2 [3,]3 [4,]4 [5,]5 [6,]6 [7,]7 [8,]8 [9,]9 [10,] 10 a[-OverloadsTesT] [1] 2 3 4 5 6 7 8 9 10 the last line does not remove the first and third element and only does the first element.? What I want to do is for zeros to return the elements and for any positive value to remove it. What I am doing wrong? B.R Alex [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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 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] why the a[-indx] does not work?
You are absolutely right it is better. This was just an example how one can convert the positives to TRUE and zeros to FALSE as it was asked. Andrija On Sun, Oct 30, 2011 at 9:57 PM, William Dunlap wdun...@tibco.com wrote: I like to use numericVector != 0 instead of is.logical(numericVector) because the former more directly indicates what you want to happen instead of relying on knowledge that numeric 0 maps to logical FALSE. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Alaios Sent: Sunday, October 30, 2011 1:40 PM To: andrija djurovic Cc: R-help@r-project.org Subject: Re: [R] why the a[-indx] does not work? I think this does the work return(m[!as.logical(data)]) I am not sure though if this is the same with return(m[!as.logical(data)]) From: andrija djurovic djandr...@gmail.com Cc: R-help@r-project.org R-help@r-project.org Sent: Sunday, October 30, 2011 9:58 PM Subject: Re: [R] why the a[-indx] does not work? as.logical(c(1,0,1,1)) [1] TRUE FALSE TRUE TRUE ?as.logical probably you mean For [-indexing only: i, j, ... can be logical vectors, indicating elements/slices to select. Such vectors are recycled if necessary to match the corresponding extent. i, j, ... can also be negative integers, indicating elements/slices to leave out of the selection. How can i convert the positives to TRUE and zeros and FALSE? From: William Dunlap wdun...@tibco.com Sent: Sunday, October 30, 2011 9:17 PM Subject: RE: [R] why the a[-indx] does not work? a[overLoadTesT==0] [1] 2 4 5 6 7 8 9 10 Look into help('[') or help('Subscript') to see how integer and logical (Boolean) subscripts differ. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto: r-help-boun...@r-project.org] On Behalf Of Alaios Sent: Sunday, October 30, 2011 11:52 AM To: R-help@r-project.org Subject: [R] why the a[-indx] does not work? Dear all, Could you please explain me why OverloadsTesT [1] 1 0 1 0 0 0 0 0 0 0 a-matrix(data=seq(1,10),nrow=10) a [,1] [1,]1 [2,]2 [3,]3 [4,]4 [5,]5 [6,]6 [7,]7 [8,]8 [9,]9 [10,] 10 a[-OverloadsTesT] [1] 2 3 4 5 6 7 8 9 10 the last line does not remove the first and third element and only does the first element.? What I want to do is for zeros to return the elements and for any positive value to remove it. What I am doing wrong? B.R Alex [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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]] [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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] generate randomly a value of a vector
Hi Boris. Here is one approach: N-100 a-rep(0,N) a[sample(N,1)]-1 a which(a==1) Look ?sample, ?which. Andrija On Thu, Sep 8, 2011 at 10:42 AM, Boris Beranger borisberan...@gmail.comwrote: Hi everyone, I have a zero vector of length N and I would like to randomly allocate the value 1 to one of the values of this vector. I presume I have to use the uniform distribution but could someone tell me how I should process? Thanks in advance, Boris -- View this message in context: http://r.789695.n4.nabble.com/generate-randomly-a-value-of-a-vector-tp3798190p3798190.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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 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] subqueries in sqlQuery function (package RODBC)
Hi R users. sorry for missing example and if question is to general but I am wondering if it is possible to execute subqueries in function sqlQuery (package RODBC) with opened connection with Excel or SQL server 2000. I couldn't find any example of this. And if it is possible what should be a correct syntax for this query: SELECT ct,COUNT(*) as n FROM (SELECT COUNT(*) AS ct FROM children GROUP BY family_id) AS x GROUP BY ct; sqlQuery(connecton, CORRECT SYNTAX ) (This query is an example from book Data Manipulation with R, Phil Spector, page 47) Thanks for any help Andrija [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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] subqueries in sqlQuery function (package RODBC)
I thought subqueries in sense of commad inside the command (in my example two select commands). It works as you proposed an I thought in this case (subqueries) that I need different syntax for sqlQury function combining SQL query and paste. But now I have another problem and again sorry if it is to general and basic but I just can't find the right option to set up. Namely, when I import table from SQL server into R, columns that are defined in SQL table as char (with leading zeros as 001, 002,...) are imported as integers. Could you, please, guide me on some options that should be set up to solve this problem? On Wed, Aug 10, 2011 at 10:16 AM, Prof Brian Ripley rip...@stats.ox.ac.ukwrote: In what sense is this a 'subquery'? It is just an SQL command (write it on one line, no terminating ;, which is not part of the query). On Wed, 10 Aug 2011, andrija djurovic wrote: Hi R users. sorry for missing example and if question is to general but I am wondering if it is possible to execute subqueries in function sqlQuery (package RODBC) with opened connection with Excel or SQL server 2000. I couldn't find any example of this. And if it is possible what should be a correct syntax for this query: SELECT ct,COUNT(*) as n FROM (SELECT COUNT(*) AS ct FROM children GROUP BY family_id) AS x GROUP BY ct; sqlQuery(connecton, CORRECT SYNTAX ) (This query is an example from book Data Manipulation with R, Phil Spector, page 47) Thanks for any help Andrija [[alternative HTML version deleted]] __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Brian D. Ripley, rip...@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~**ripley/http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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] Simple loop
Hi. There is no need to do this in a for loop. Here is one approach: x - read.table(textConnection(Site Prof H 1 1 24 1 1 16 1 1 67 1 2 23 1 2 56 1 2 45 2 1 67 2 1 46), header = TRUE) closeAllConnections() x cbind(x,newCol=unlist(tapply(x[,3],paste(x[,1],x[,2],sep=), function(x) x-min(x))) Site Prof H newCol 11111 24 8 11211 16 0 11311 67 51 12112 23 0 12212 56 33 12312 45 22 21121 67 21 21221 46 0 Andrija On Tue, May 3, 2011 at 5:44 PM, Woida71 w.gost...@ipp.bz.it wrote: Hello everybody, I am beginning with loops and functions and would be glad to have help in the following question: If i have a dataframe like this Site Prof H 1 1 24 1 1 16 1 1 67 1 2 23 1 2 56 1 2 45 2 1 67 2 1 46 And I would like to create a new column that subtracts the minimum of H from H, but for S1 and P1 only the minimum of the data points falling into this category should be taken. So for example the three first numbers of the new column write: 24-16, 16-16, 67-16 the following numbers refering to Site1 and Prof2 write: 23-23, 56-23, 45-23. I think with two loops one refering to the Site, the other to the Prof, it should be possible to automatically create the new column. Thanks a lot for any help. -- View this message in context: http://r.789695.n4.nabble.com/Simple-loop-tp3492819p3492819.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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 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] Simple loop
William, you are right. Thanks for clarification. Andrija On Tue, May 3, 2011 at 9:04 PM, William Dunlap wdun...@tibco.com wrote: Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of andrija djurovic Sent: Tuesday, May 03, 2011 11:28 AM To: Woida71 Cc: r-help@r-project.org Subject: Re: [R] Simple loop Hi. There is no need to do this in a for loop. Here is one approach: x - read.table(textConnection(Site Prof H 1 1 24 1 1 16 1 1 67 1 2 23 1 2 56 1 2 45 2 1 67 2 1 46), header = TRUE) closeAllConnections() x cbind(x,newCol=unlist(tapply(x[,3],paste(x[,1],x[,2],sep=), function(x) x-min(x))) Site Prof H newCol 11111 24 8 11211 16 0 11311 67 51 12112 23 0 12212 56 33 12312 45 22 21121 67 21 21221 46 0 That works when Site and Prof are ordered as shown, but if they are not sorted cbind(...,tapply) won't line up the the new entries with the old rows properly. Try doing it on x[8:1,] to see this. ave() can deal that problem: cbind(x, newCol2 = with(x, ave(H, Site, Prof, FUN=function(y)y-min(y Site Prof H newCol2 111 24 8 211 16 0 311 67 51 412 23 0 512 56 33 612 45 22 721 67 21 821 46 0 Warning message: In min(y) : no non-missing arguments to min; returning Inf The warning is unfortunate: ave() calls FUN even for when there is no data for a particular group (Site=2, Prof=2 in this case). Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com Andrija On Tue, May 3, 2011 at 5:44 PM, Woida71 w.gost...@ipp.bz.it wrote: Hello everybody, I am beginning with loops and functions and would be glad to have help in the following question: If i have a dataframe like this Site Prof H 1 1 24 1 1 16 1 1 67 1 2 23 1 2 56 1 2 45 2 1 67 2 1 46 And I would like to create a new column that subtracts the minimum of H from H, but for S1 and P1 only the minimum of the data points falling into this category should be taken. So for example the three first numbers of the new column write: 24-16, 16-16, 67-16 the following numbers refering to Site1 and Prof2 write: 23-23, 56-23, 45-23. I think with two loops one refering to the Site, the other to the Prof, it should be possible to automatically create the new column. Thanks a lot for any help. -- View this message in context: http://r.789695.n4.nabble.com/Simple-loop-tp3492819p3492819.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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 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 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] adding a name to cross tab margins
Hi. Have a look also at ?addmargins. addmargins(table(fS,fF),c(1,2),FUN=list(total.row=sum,total.col=sum)) Andrija On Fri, Apr 15, 2011 at 3:29 PM, Dmitry Berman ravenb...@gmail.com wrote: Listers, I have created a cross-tab matrix using the following code: S - c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,1,2,3,1,1,2,3,1,2,2) F - c(1,2,3,1,2,3,1,1,1,1,2,3,1,1,3,2,2,2,1,2,3,1,1,1,1,2,3,3,1,3,1,3,1,1,2,3,1,1,3,2,3,2,1,1,1,2,3,1,1,2) table(S,F) fF -factor(F, labels = c(Dem,Ind,Rep)) fS -factor(S, labels = c(Dem,Ind,Rep)) PiD - table(fS,fF) PiD - cbind(PiD, margin.table(PiD,1)) PiD - rbind(PiD, margin.table(PiD,2)) Now I would like to add the Column Name and Row Name Total to the last row and last column (where the marginal totals now hang out). When I use the command colnames(PiD[,4]) - c(total) I get the error: Error in `colnames-`(`*tmp*`, value = WTF) : attempt to set colnames on object with less than two dimensions Since I am still new to R, I can't really understand the error... Can someone tell me what I am doing wrong here? Thanks [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.htmlhttp://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 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] adding a name to cross tab margins
Sorry, I've just reversed the names of dimensions :) addmargins(table(fS,fF),c(1,2),FUN=list(total.col=sum,total.row=sum)) Andrija On Fri, Apr 15, 2011 at 3:50 PM, andrija djurovic djandr...@gmail.comwrote: Hi. Have a look also at ?addmargins. addmargins(table(fS,fF),c(1,2),FUN=list(total.row=sum,total.col=sum)) Andrija On Fri, Apr 15, 2011 at 3:29 PM, Dmitry Berman ravenb...@gmail.comwrote: Listers, I have created a cross-tab matrix using the following code: S - c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,1,2,3,1,1,2,3,1,2,2) F - c(1,2,3,1,2,3,1,1,1,1,2,3,1,1,3,2,2,2,1,2,3,1,1,1,1,2,3,3,1,3,1,3,1,1,2,3,1,1,3,2,3,2,1,1,1,2,3,1,1,2) table(S,F) fF -factor(F, labels = c(Dem,Ind,Rep)) fS -factor(S, labels = c(Dem,Ind,Rep)) PiD - table(fS,fF) PiD - cbind(PiD, margin.table(PiD,1)) PiD - rbind(PiD, margin.table(PiD,2)) Now I would like to add the Column Name and Row Name Total to the last row and last column (where the marginal totals now hang out). When I use the command colnames(PiD[,4]) - c(total) I get the error: Error in `colnames-`(`*tmp*`, value = WTF) : attempt to set colnames on object with less than two dimensions Since I am still new to R, I can't really understand the error... Can someone tell me what I am doing wrong here? Thanks [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.htmlhttp://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 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] Converting a categorical variable to multiple dichotemous variables
hi: here is one solution: cat-as.factor(c(1,1,3,2,4)) model.matrix(~cat-1,cat) cbind(cat,model.matrix(~cat-1,cat)) Andrija On Tue, Apr 12, 2011 at 2:17 PM, Shane Phillips sphill...@lexington1.netwrote: I have a categorical variable in a dataframe similar to the following... cat 1 1 3 2 4 I need to convert it to 4 dichotemous variables for each observations like... cat1cat2cat3cat4 1 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 Thanks in advance! Shane __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.htmlhttp://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 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] Creating a symmetric contingency table from two vectors with different length of levels in R
Hi: Here is one solution: a-factor(c(1,2,4,5,6)) b-factor(c(2,2,4,5,5)) b1-factor(b,levels=c(levels(b),levels(a)[levels(a)%in%levels(b)==FALSE])) table(a,b1) but be aware that levels of b is a subset of levels of a. Andrija On Wed, Apr 6, 2011 at 10:39 AM, suparna mitra mi...@informatik.uni-tuebingen.de wrote: Hello, How can I create a symmetric contingency table from two categorical vectors having different length of levels? For example one vector has 98 levels TotalData1$Taxa.1 [1] Aconoidasida Actinobacteria (class) Actinopterygii Alphaproteobacteria [5] AmoebozoaAmphibia Anthozoa Aquificae (class) and so on . 98 Levels: Aconoidasida Actinobacteria (class) and the other vector has 105 levels TotalData1$Taxa.2 [1] FlavobacteriaProteobacteria Bacteroidetes/Chlorobi group Bacteria [5] EpsilonproteobacteriaEpsilonproteobacteria Epsilonproteobacteria and so on .. 105 Levels: Acidobacteria Aconoidasida Actinobacteria (class) Now I want to create a symmetric contingency table. Any quick idea will be really helpful. Best regards, Mitra [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.htmlhttp://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 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] Value between which elements of a vector?
Hi: try this and have a look at ?cut just to change the lables: data.frame( weeks=sprintf('%s %d','week',1:30), specialweeks=cut(1:30,c(0,2,5,12,18,19,20),right = FALSE)) Andrija On Tue, Apr 5, 2011 at 1:20 PM, beatleb rhelpfo...@gmail.com wrote: Dear R-useRs, I am looking for a why to perform the following: specialweeks-c(0,2,5,12,18,19,20) weeks-c(1:30) Now I would like that for every week it is even between which elements of vector special weeks it is. For weeks after 20, the value NA or 20, or even 20-30is fine. Thus for week 1: 0-2 week 2: 2-5 week 3: 2-5 week 4: 2-5 week 5: 5-12 ect It is not relevant if those intervals are captured in a matrix, in a vector or whatever. I hope that you can help me! With best regards, Brenda Grondman -- View this message in context: http://r.789695.n4.nabble.com/Value-between-which-elements-of-a-vector-tp3427751p3427751.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.htmlhttp://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 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 splitting ists into sub-lists
Hi: here is one solution. Not so elegant but maybe it will give you some ideas: mylist1-rep(mylist,c(2,2)) a-matrix(c(index1,!index1,index2,!index2),ncol=4) mylist2-list() for (i in 1:4) { mylist2[[i]]-mylist1[[i]][,a[,i]] } mylist2 Andrija On Sun, Apr 3, 2011 at 6:56 PM, Axel Urbiz axel.ur...@gmail.com wrote: Dear List, Let's say I have a list whose components are 2 matrices (as exemplified in the mylist object below). I'd like to create a list with components being 4 matrices based on an logical index vector. is there a way to simplify what I'm doing to obtain the results in mylist2? I'd like something that would work on an arbitrary number of elements in mylist. mylist - list(matrix(1:9,3,3), matrix(10:18,3,3)) index1 - c(TRUE,FALSE,TRUE) index2- c(FALSE,TRUE,TRUE) mylist2 - list(mylist[[1]][,index1], mylist[[1]][,index1==FALSE], mylist[[2]][,index2], mylist[[2]][,index2==FALSE]) Thanks in advance, Axel. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] Matrix manipulation
HI, here is another solution: int - sample(1:20,10) int [1] 10 4 5 2 14 17 9 11 16 13 mat-matrix(11:30,ncol=4) mat [,1] [,2] [,3] [,4] [1,] 11 16 21 26 [2,] 12 17 22 27 [3,] 13 18 23 28 [4,] 14 19 24 29 [5,] 15 20 25 30 mat[apply(mat,1, function(x) any(int==x[1])),] [,1] [,2] [,3] [,4] [1,] 11 16 21 26 [2,] 13 18 23 28 [3,] 14 19 24 29 Andrija On Sat, Apr 2, 2011 at 7:08 AM, Joseph N. Paulson josephpaul...@gmail.comwrote: Hi all! I have a vector, let's say for example int - sample(1:20,10); for now: now I have a matrix... M = m x n where the first column is a feature column and most likely shares at least one of the int (interesting) numbers. I want to extract the rows where int[] = M[,1] I thought: rownames(int)-int; rownames(M)-M[,1]; M[rownames(int),] would work, but it doesn't... (I assume because I have rownames(int) that are not found in M[,1]. Neither does, rownames(M)==rownames(int)... Any help would be greatly appreciated! Thank you! [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 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] export list to csv
Hi everybody. I have list like this: l-list(data.frame(q1=c(1,2,check),q2=c(3,check,5)), data.frame(q1=c(check,1),q2=c(4,5))) names(l)-c(A,B) rownames(l[[1]])-c(aa,bb,cc) rownames(l[[2]])-c(aa,bb) Every object has the same number of columns but different number of rows. Does anyone know if it is possible to export such kind of list, into one csv file, and keeping all the names? Thanks in advance Andrija [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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] export list to csv
Soryy, I didn't explain well what I want. I would like to have a table in csv on txt file like this: $A q1q2 aa 1 3 bb 2 check cc check 5 $B q1 q2 aa check 4 bb 1 5 The same as write.csv of any data frame. On Wed, Mar 16, 2011 at 4:03 PM, Henrique Dallazuanna www...@gmail.comwrote: Use dput: dput(l, file = l_str.txt) Then, to read again: l - dget(file = 'l_str.txt') On Wed, Mar 16, 2011 at 11:55 AM, andrija djurovic djandr...@gmail.com wrote: Hi everybody. I have list like this: l-list(data.frame(q1=c(1,2,check),q2=c(3,check,5)), data.frame(q1=c(check,1),q2=c(4,5))) names(l)-c(A,B) rownames(l[[1]])-c(aa,bb,cc) rownames(l[[2]])-c(aa,bb) Every object has the same number of columns but different number of rows. Does anyone know if it is possible to export such kind of list, into one csv file, and keeping all the names? Thanks in advance Andrija [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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] export list to csv
Thanks everyone for different solutions. Every solution works very well. For my purpose, this function sink does what I was looking for. Andrija On Wed, Mar 16, 2011 at 4:23 PM, Roman Luštrik roman.lust...@gmail.comwrote: How about? sink(andrija.csv) l sink() -- View this message in context: http://r.789695.n4.nabble.com/export-list-to-csv-tp3381992p3382062.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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 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] data.frame transformation
Thank you Bill for this additional solution. Andrija On Tue, Mar 15, 2011 at 12:16 AM, bill.venab...@csiro.au wrote: It is possible to do it with numeric comparisons, as well, but to make life comfortable you need to turn off the warning system temporarily. df - data.frame(q1 = c(0,0,33.33,check), q2 = c(0,33.33,check,9.156), q3 = c(check,check,25,100), q4 = c(7.123,35,100,check)) conv - function(x, cutoff) { oldOpt - options(warn = -1) on.exit(options(oldOpt)) x - as.factor(x) lev - as.numeric(levels(x)) levels(x)[!is.na(lev) lev cutoff] - . x } Check: (df1 - data.frame(lapply(df, conv, cutoff = 10))) q1q2q3q4 1 . . check . 2 . 33.33 check35 3 33.33 check25 100 4 check . 100 check Bill Venables. -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of David Winsemius Sent: Tuesday, 15 March 2011 6:29 AM To: andrija djurovic Cc: r-help@r-project.org Subject: Re: [R] data.frame transformation On Mar 14, 2011, at 3:51 PM, andrija djurovic wrote: I would like to hide cells with values less the 10%, so . or just doesn't make me any difference. Also I used apply combined with as.character: apply(df, 2, function(x) ifelse(as.character(x) 10,.,x)) This is, probably not a good solution, but it works except that I lose row names and because of that I was wondering if there is some other way to do this. Anyway thank you both i will try to do this before combining numbers and strings. I saw your later assertion that it didn't work which surprised me. My version of your data followed my advice not to use factors and your effort did succeed when the columns were character rather than factor. I put back the row numbers by coercing back to a data.frame. `apply` returns a matrix. df-data.frame(q1=c(0,0,33.33,check),q2=c(0,33.33, check,9.156), + q3=c(check,check,25,100),q4=c(7.123,35,100,check), stringsAsFactors=FALSE) as.data.frame(apply(df, 2, function(x) ifelse(as.character(x) 10,.,x))) q1q2q3q4 1 . . check 7.123 2 . 33.33 check35 3 33.33 .25 100 4 check 9.156 100 check There is a danger of using character collation in that if there are any leading characters in those strings that are below 1 such as a blank or any other punctuation, they will get dotted. , 1 [1] TRUE . 1 [1] TRUE - 1 [1] TRUE And 1.check would also get dotted 1.check 10 [1] TRUE Andrija On Mon, Mar 14, 2011 at 8:11 PM, David Winsemius dwinsem...@comcast.net wrote: On Mar 14, 2011, at 2:52 PM, andrija djurovic wrote: Hi R users, I have following data frame df-data.frame(q1=c(0,0,33.33,check),q2=c(0,33.33,check,9.156), q3=c(check,check,25,100),q4=c(7.123,35,100,check)) and i would like to replace every element that is less then 10 with . (dot) in order to obtain this: q1q2q3q4 1 . . check . 2 . 33.33 check35 3 33.33 check25 100 4 check . 100 check I had a lot of difficulties because each variable is factor. Right, so comparisons with will throw an error. I would sidestep the factor problem with stringsAsFactors=FALSE in the data.frame call. You might want to reconsider the . as a missing value. If you are coming from a SAS background, you should try to get comfortable with NA or NA_character as a value. df-data.frame(q1=c(0,0,33.33,check),q2=c(0,33.33,check,9.156), q3=c(check,check,25,100),q4=c(7.123,35,100,check), stringsAsFactors=FALSE) is.na(df) - t(apply(df, 1, function(x) as.numeric(x) 10)) Warning messages: 1: In FUN(newX[, i], ...) : NAs introduced by coercion 2: In FUN(newX[, i], ...) : NAs introduced by coercion 3: In FUN(newX[, i], ...) : NAs introduced by coercion 4: In FUN(newX[, i], ...) : NAs introduced by coercion df q1q2q3q4 1 NA NA check NA 2 NA 33.33 check35 3 33.33 check25 100 4 check NA 100 check Could someone help me with this? Thanks in advance for any help. Andrija [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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. David Winsemius, MD West Hartford, CT David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list 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
Re: [R] applying to dataframe rows
Hi, maybe this: df-data.frame(a=c(1,2,3,Inf,4,Inf),b=c(Inf,2,3,4,5,8)) df[apply(df,1, function(x) !any(x==Inf)),] df[apply(df,1, function(x) any(x==Inf)),] Andrija On Tue, Mar 15, 2011 at 10:44 PM, Alexy Khrabrov delivera...@gmail.comwrote: How do I apply a function to every row of a dataframe most naturally? Specifically, I'd like to filter out any row which contains an Inf in any column. Since all columns are numeric, I guess max should work on a row... -- Alexy __ R-help@r-project.org mailing list 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 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.