r

Sweave not printing localized characters

I'm trying to incorporate some plots from R in my LaTeX document through Sweave. \SweaveOpts{eps = FALSE, pdf = TRUE, echo = FALSE, prefix = TRUE, prefix.string = data} <<label = abundanca:barplot, fig = TRUE, include = FALSE, results = hide>>= barplot(abund, xlab="Vzorčne postaje", ylab="Abundanca", main="", col="slategrey", names.arg...

how to calculate "consecutive mean" in R without using loop, or in a more efficient way?

I have a set a data that I need to calculate their "consecutive mean" (I dunno if it is the correct name, but I can't find anything better), here is an example: ID Var2 Var3 1 A 1 2 A 3 3 A 5 4 A 7 5 A 9 6 A 11 7 B 2 8 B 4 9 B 6 10 B 8 11 B 10 Here I need to calculat...

Extract values from data frame in R

Using R, I would like to find out which Samples (S1, S2, S3, S4, S5) fulfill the following criteria:contain minimally one value (x, y or z) bigger than 4. Thanks, Alex. Sample x y z <br> S1 -0.3 5.3 2.5 <br> S2 0.4 0.2 -1.2 <br> S3 1.2 -0.6 3.2 <br> S4 4.3 0.7 5.7 <br> S5 2.4 4.3 2.3 <br> ...

apply operation to each element in column matrix in R

I want to apply the max operation to each column entry in R. But when I do the following, it applies the operation across all columns. Is there way to do this without using for loops? > s [1] 750.0 975.0 1125.0 1237.5 1312.5 1400.0 > max(1050-s,0) [1] 300 ## expect result to be (300 150 0 0 0 0) ...

Exporting R tables to HTML

Is there a way to easily export R tables to a simple HTML page? ...

Logistic Regression in R (SAS-like output)

I have a problem at hand which I'd think is fairly common amongst groups were R is being adopted for Analytics in place of SAS. Users would like to obtain results for logistic regression in R that they have become accustomed to in SAS. Towards this end, I was able to propose the Design package in R which contains many functions to extra...

Read in multiple .txt files with header in R

Okay, I'm trying to use this method to get my data into R, but I keep on getting the error: Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : line 1 did not have 22 elements This is the script that I'm running: library(foreign) setwd("/Library/A_Intel/") filelist <-list.files() #assuming tab sepa...

Calculating Summary Statistics by Day/Week/Month

In R, I have a data-frame of various statistics recorded throughout the day. (For example, heart-rate) . The time-stamps for each measurement-entry are automatically created, and I have already converted them into a POSIXt class element. The number of observations varies from day to day. I am wondering how I can calculate summary sta...

Superfluous data at the beginning of a R header

I'm using the following script to read in .txt files into R. For some reason, even though I only have 21 elements in the header it claims that I have 22. This makes the read table function bug because the rest of the lines of the file only have 21 items. When I use the scan function I notice that my header actually starts at element 2 ...

assign variables in apply()-d function using <<-

In R, I want to call apply on a huge data.frame, and write values back into specific positions of other dataframes. However, using '<<-' only works when I call the apply function from the global environment. As I understand, '<<-' searches the parent.env() for the variable. Why is the parent environment of the function called in bar() n...

Functions and try() in R

Still struggling with R and then in particular with errorhandling: If I use: result<-try(sqlSave(ch,df,tablename="tblTest")) I can use: if (class(result) != "try-error") to check if somenthing went wrong. No problem. But if I use try in combination with a function it doesn't work as I expected: result <- try(ch<-odbcConnect("TE...

In R, how do you evaluate ... in the calling function?

If I want to know what is stored in a ... argument within an R function, I can simply convert it to be a list, like so foo <- function(...) { dots <- list(...) print(dots) } foo(x = 1, 2, "three") #$x #[1] 1 # #[[2]] #[1] 2 # #[[3]] #[1] "three" What I can't figure out is how to evaluate ... in the calling function. In this next...

R inline compilation of C code fails on Windows

Within the Windows XP Pro RGui, can't compile inline C code. Get error: 'sh' is not recognized as an internal or external command Clearly there is a configuration error, but can't find a way to resolve it either in R documentation or via googling. Must be a simple solution! The same R code works fine on linux: the inline C compiles ...

RGoogleDocs (or RCurl) giving SSL certificate problem

I was using one of my favorite R packages today to read data from a google spreadsheet. It would not work. This problem is occurring on all my machines (I use windows) and it appears to be a new problem. I am using Version: 0.4-1 of RGoogleDocs library(RGoogleDocs) ps <-readline(prompt="get the password in ") sheets.con = getGoogleDocsC...

R, python or octave: empirical quantile (inverse cdf) with confidence intervals?

I'm looking for a built-in function that returns the sample quantile and an estimated confidence interval in something other than MATLAB (MATLAB's ecdf does this). I'm guessing R has this built-in and I just haven't found it yet. If you have any standalone code to do this, you could also point to it here, though I hope to find somethin...

how to concatenate factors?

I was surprised to see that R will coerce factors into a number when concatenating vectors. This happens even when the levels are the same. For example: > facs <- as.factor(c("i", "want", "to", "be", "a", "factor", "not", "an", "integer")) > facs [1] i want to be a factor not an integer Levels: a an be...

Formatting Decimal places in R

I have a number, for example 1.128347132904321674821 that I would like to show as only two decimal places when output to screen (or written to a file). How does one do that? x <- 1.128347132904321674821 EDIT: The use of: options(digits=2) Has been suggested as a possible answer. Is there a way to specify this within a script fo...

Counts & Percentages in xTable, Sweave, R, cross tabulations

Edit: Building off of aL3xa's answer below, I've modified his syntax below. Not perfect, but getting closer. I still haven't found a way to make xtable accept \multicolumn{} arguments for columns or rows. It also appears that Hmisc handles some of these type of tasks behind the scenes, but it looks like a bit of an undertaking to underst...

How to use the R Function 'Sweep'

When i look at the source of R Packages, i see the function sweep used quite often. Sometimes it's used when a simpler function would have sufficed (e.g., 'apply'), other times, it's impossible to know exactly what it's is doing without spending a fair amount of time to step through the code block it's in. the fact that i can reproduce ...

Factors in R: more than an annoyance?

One of the basic data types in R is factors. In my experience factors are basically a pain in the ass and I never use them. I always convert to characters. I feel oddly like I'm missing something. Are there a lot of functions that use factors as grouping variables? When should I be using factors? Do you use them? ...