spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories
  Home » R » Page 1
supplying user defined color in r circlize package
in panel.fun, region and value correspond to the data on each chromosome, so you cannot use bed1$clr because bed1$clr corresponds to the whole data. actually, in panel.fun, value contains columns excluding the first three columns in bed1 and only contains data for the current chromosome (remember panel.fun applied on each chromosome), so you can change your code to: circos.genomicTrackPlotRegion

Categories : R

How to extract a date from a nc file name?
From a vector of strings like this you can extract the date-time with strptime: f <- "ext_20100929T235959_272_001_7_1.nc" strptime(f, "ext_%Y%m%dT%H%M%S", tz = "GMT") ## [1] "2010-09-29 23:59:59 GMT" dt <- as.POSIXct(strptime(f, "ext_%Y%m%dT%H%M%S", tz = "GMT")) (Assuming the tokenizing is in that format see - help(strptime) for details. ) You can put that "dt" vector into a data

Categories : R

split dataframe by row number in R
Assuming that rows 1&2 goes in the first split, 3,4,5,6 in the second and 7 to nrow(df) goes in the last split(df, cumsum(1:nrow(df) %in% v)) but if 1:3 rows are in the first split, then comes 4:7, and in the third 8 to nrow(df) split(df, cumsum(c(TRUE,(1:nrow(df) %in% v)[-nrow(df)])) ) Or as @James mentioned in the comments, split(df, cumsum(1:nrow(df) %in% (v+1)))

Categories : R

Aggregate and DCast
You don't need the aggregateNStatus dcast.data.table(test, betnr+year~status, length, fill=NA_integer_, value.var='status') # betnr year SHORT fire hire normal #1: 50536224 1996 NA NA 2 NA #2: 50536224 1997 NA NA NA 2 #3: 50536224 1998 1 1 NA 1 #4: 50536224 1999 NA 1 NA NA

Categories : R

Using ggplot2 to create dodged chart
First thing you'll want to do is rearrange your data for ggplot - it needs to be long and not wide. To do this, we'll us the tidyr package. library(tidyr) library(dplyr) library(ggplot2) So here's your data now, df_untidy=data.frame(test_number=1:3,instance_1=c(2,3,5),instance_2=c(3,6,9)) test_number instance_1 instance_2 1 1 2 3 2 2 3 6

Categories : R

Connecting to LinkedIn via ouath and R
Use httr instead. Hadley has an example in his package: https://github.com/hadley/httr/blob/master/demo/oauth2-linkedin.r Here's example library("httr") myapp <- oauth_app(appname = "scottsapp", key = "<key>", secret = "<secret>") TokenLinkedIn <- R6::R6Class("TokenLinkedIn", inherit = Token2.0, list( sign = function(method, url) { url <- parse_url(url) url$query$o

Categories : R

Plotting pre aggregated data
Since no one had a response for ggplot, I used rep to re-expand and sample the data. So, if I had 18 million observations originally, I used 180,000 for the times argument of rep, and multiplied by this by my previously calculated proportion of the data. I'm not sure what the threshold would then be for the times argument (if it's less than 1 will no data point be created?). This means I lose th

Categories : R

image fails to appear in .Rpres
The following (in file file.Rpres) works for me, with rstudio. Title slide =========== author: me date: now Test slide ========== Below is a test image ![my image](testimage.png)

Categories : R

How to save multiple numbers in one cell in a matrix/dataframe?
This is very unusual format so things can get messy: dat <- read.table(header = TRUE, text="row.names row day Tx Hx Tn 1 61 61 30 31.9 36.85 19.1 2 84 84 23 33.5 43.07 20.3 3 85 85 24 31.5 39.82 19.2 4 94 94 2 30.9 41.36 20.0 5 99 99 7 34.0 43.17 21.6 6 101 101 9 34.4 42.45

Categories : R

Can ggplot change the direction of axis.ticks from downward to upward?
I think this achieves your goal: library(ggplot2) library(grid) gg <- ggplot(mtcars, aes(mpg, drat)) gg <- gg + geom_point(size=3) gg <- gg + theme_bw() gg <- gg + theme(axis.text.x=element_text(size=17, vjust=-0.25, color="black")) gg <- gg + theme(axis.text.y=element_text(size=17, hjust=1, color="black")) gg <- gg + theme(axis.ticks=element_line(color="black", size=0.5)) gg &

Categories : R

sapply() and ifelse() in R
In all honesty, with a situation like this I would not use ifelse and instead use the if and else components separately. ifelse only returns a value of the same shape as your test (hence your initial output only providing 3 outputs) as you can see explained in this question. I cannot think of concise way to get all the combinations tested without some regex which seems nothing more than an unnee

Categories : R

Define a callback for command start
There's no real way to profile "top-level commands" because R doesn't know what those are. How are you defining them? If system.time is not granular enough for you (because it doesn't look at individual pieces of code), my suggestion would be to go in the opposite direction and use Rprof, which allows you to start and stop the timing of expressions and, on output (summaryRprof()) prints a breakdo

Categories : R

Shiny plotGoogleMaps Internet Explorer vs Chrome
Your problem is not R, shiny or plotGoogleMaps, but IE support for html5 standard. IE support for srcdoc is not good, read from this link. You may use polyfill to support IE but I do not think it is necessary since you are already creating necessary html file in plotGoogleMaps step. Try following code. Instead of giving iframe srcdoc, I use src property. Also google map html is created in www d

Categories : R

Finding and labelling candidates/ouliers outside a curve in R plot
I will speak about your initial volcano-type-graph problem and not the made up one because they are totally different. So I really thought this a lot and I believe I reached a solid conclusion. There are two options: 1. You know the equations of the lines, which would be really easy to work with. 2. You do not know the equation of the lines which means we need to work with an approximation. Some

Categories : R

Fill a data.frame with loop, levels sets are different
Well, my recommendation would be twofold. First, just don't use factors when creating the data.frame; you can factor post-hoc if you really need them. So: GammaShape <- data.frame(Format, Shape, stringsAsFactors = FALSE) Second: for loops are a really inefficient way to modify data.frames, because of how data.frames are stored and modified: every time you tweak a value, you're actually recre

Categories : R

How to create a heatmap in R with data frames
There is a new package out called heatmap.plus that may help you. It allows for a wider set of input formats than the original heatmap package. Documentation is here: http://cran.r-project.org/web/packages/heatmap.plus/heatmap.plus.pdf You can also do very nice heatmaps using ggplot2. The geom_tile expression is the one you want. Here is an example (with code) that probably looks pretty close to

Categories : R

How to remove/eliminate outliers using IQR method
You are looking at the right function boxplot.stats to look at what a function in R you can use ?functionName so try ?boxplot.stats and you will see that it return the outliers values in a slot call out Value: List with named components as follows: stats: a vector of length 5, containing the extreme of the lower whisker, the lower ‘hinge’, the median, the upper ‘

Categories : R

R try catch block
I cannot really test it, but can you try replacing your if (TRUE) condition with this: tryCatch({ # Recursive Partitioning and Regression Trees cat('Recursive Partitioning and Regression Trees (rpart)',' ') rtree <- rpart(fmla,data=treeData) # <-- NEED TRY CATCH HERE... print(summary(rtree)) cat('Confusion matrix for rpart') print(table(predict(rtree), treeData[[cName]])) },

Categories : R

find row in data frame with closest match in R
df<-data.frame(Age=c(23,29,45,24,28),Occupation=c("Builder","Worker","Contractor","Engineer","Doctor"),Nationality=c("German","British","Vietnamese","German","Indian"),stringsAsFactors=F) newdata<-c(23,"Doctor","German") df[which.max(apply(df,1,function(vec,dat){sum(vec==dat)},newdata)),] Age Occupation Nationality 1 23 Builder German In case of ties, you can have all better

Categories : R

How to illustrate non available data points in a different shape using ggplot2?
This comes pretty close, I think. ggplot(dataset, aes(x=Pos, y=variable, color=cut(value, breaks=10), shape=ifelse(is.na(value),"Missing","Present"))) + geom_point(size=4) + geom_line() + scale_shape_manual(name="",values=c(Missing=4,Present=19))+ scale_color_discrete(labels=c("0-10%","10-20%","20-30%","30-40%","40-50%","50-60%","60-70%","70-80

Categories : R

Recoding of data
Just to add clarity to @BenBolker's comment above: df = data.frame("original" = 1:360) df$category = cut(df$original, breaks = c(0, 22.5, 67.5, 112, 158, 202, 248, 292, 338, 360), labels = 1:9) summary(df$category)

Categories : R

How to look up values in R based on row and column
It's called reshaping a data.frame from wide to long format. One option to do that would be, using the package tidyr: library(tidyr) gather(df, Grp, Value, -name) # name Grp Value #1 A val1 2 #2 B val1 5 #3 C val1 7 #4 A val2 3 #5 B val2 6 #6 C val2 9

Categories : R

data layout for faceting in ggplot
What you are actually trying to do is reshaping your data and them plotting it. Now you have done this manually, but this can be done programmaticaly with the dplyr and tidyr packages: library(dplyr) library(tidyr) data2 <- data %>% gather(Parameter, Combo, c(Param1,Param2)) ggplot(data2, aes(x=Drug, y=Combo, color=Parameter)) + geom_point(size=3) + facet_grid(Parameter~Sample) + the

Categories : R

copy values of a column into another column based on a condition using a loop
You can use base R indx <- with(mydata, ave(as.numeric(dstart1), id, FUN=function(x) c(x[-1]==x[-length(x)], FALSE))) transform(mydata, offence2=ifelse(!!indx, c(as.character(offence[-1]), NA), NA)) Or using dplyr library(dplyr) mydata %>% group_by(id) %>% mutate(offence2= dstart1==lead(dstart1), offence2= ifelse(!is.na(offence2)&offe

Categories : R

Acyclic graph - Smallest weight of each path between vertices
get.shortest.paths(..., output="epath") will give you the indices of the edges involved in each of the shortest paths that it finds in the $epath component of the result items. You can then use min(E(g)$weight[some.path$epath]) to get the minimum weight in the path (where some.path is one of the paths returned by get.shortest.paths). Update: okay, if your graph is not acyclic, there can be more t

Categories : R

Performing a weighted distance between matrix rows with matrix operators in R
You can use the dist function here. First divide each column by its standard deviation to do the scaling you want. mat2<-apply(mat,2,function(x)x/sd(x)) x<-dist(mat2)^2/9 x # Australia Austria Brazil # Austria 1.2201838730 # Brazil 2.2438097238 2.6753385616 # Canada 0.5118006068 1.9591707783 3.3896964564 To recover an ordinar

Categories : R

Exporting the SVM Results from R
Why not do your GUI also in R by using R Shiny? Working simple GUI for SVM in R: # Graphical user interface for support vector machines # If you don't have r shiny installed: install.packages("shiny") library(shiny) library(e1071) library(MASS) server <- function(input, output) { output$svmSummary <- renderPrint({ data(cats) svmmodel <- svm(Sex~., data = cats) print(sum

Categories : R

Plot ages in a histogram using birthdays
Perhaps something like this to start: a=c("08.11.1979","17.03.1990","19.08.1990","20.06.1986","13.02.1980") A=data.frame(DOB=as.Date(a,format='%d.%m.%Y')) A$AGE=as.numeric(as.Date(Sys.time())-A$DOB)/365 hist(A$AGE) You will plot the age in years, looking like this: Is this on the right direction?

Categories : R

What does %>% function mean in R?
%...% operators. %>% has no builtin meaning but the user (or a package) is free to define operators of the form %whatever% in any way they like. For example, this function will return a string consisting of its left argument followed by a comma and then it's right argument. "%,%" <- function(x, y) paste(x, y, sep = ",") # test run "Hello" %,% "World" ## [1] "Hello,World" The base of R

Categories : R

Getting error "variable lengths differ (found for 'columns_features')" in R
From the ?as.formula examples: ## Create a formula for a model with a large number of variables: xnam <- paste0("x", 1:25) (fmla <- as.formula(paste("y ~ ", paste(xnam, collapse= "+")))) Which implies that in your case the following should work: formulas <- as.formula(paste("train$left_eye_center_x ~", paste(colnames(train)[31:50], collapse = "+"))) A work-around, instead of usin

Categories : R

How to randomly pick a number of combinations from all the combinations efficiently?
This is not exactly what you need but perhaps it can get you started: library(data.table) #to make the table easier members=1:3000; X=data.table(RUN=1:5000) X<-X[,as.list(sample(members, 3)),by=RUN] This will create 3 new columns that are randomly selected from the members vector. See them as IDs of each member. I would do a check to see how many as unique using: X[duplicated(X, by=c(

Categories : R

Implement multi class classification using SVM in R
Found the answer to my query above. I implemented one vs rest classifier by building binary classifiers on iris data present by default in R. It has 3 classes. So, I built 3 binary classifiers. Below is the code: data(iris) head(iris) table(iris$Species) nrow(iris) index_iris<-sample.split(iris$Species,SplitRatio=.7) trainset_iris<-iris[index_iris==TRUE,] testset_iris<-iris[index_iris==F

Categories : R

R reshape cast error
The smithsm dataset doesn't have time column. It is not clear what the expected wide form is. Perhaps, this helps library(reshape2) dcast(smithsm, subject~variable, value.var='value') # subject age height time weight #1 John Smith 33 1.87 1 90 #2 Mary Smith NA 1.54 1 NA data smithsm <- structure(list(subject = c("John Smith", "Mary Smith", "John Smith", "Mar

Categories : R

2D irregular aggregation of a matrix
This answer provides a great starting point using tapply: b <- melt(a) bb <- with(b, tapply(value, list( y=cut(Var1, breaks=c(0, breaks, Inf), include.lowest=T), x=cut(Var2, breaks=c(0, breaks, Inf), include.lowest=T) ), sum) ) bb # x # y [0,12] (12,14] (14,25] (25,60] (60,71] (71,89] (89,Inf] # [0,12] 297 48 260 825 242

Categories : R

Why is c(1:10)[NA] the same as rep(10,NA)?
This form of indexing is specifically mentioned in the docs (see ?"["): NAs in indexing When extracting, a numerical, logical or character NA index picks an unknown element and so returns NA in the corresponding element of a logical, integer, numeric, complex or character result, and NULL for a list. (It returns 00 for a raw result.)

Categories : R

Annotating ggplots inside a loop geom_hline is wrong
I believe this problem is related to List for Multiple Plots from Loop (ggplot2). So as mentioned there, the only thing you need to do is replace aes(...) with aes_string(...).

Categories : R

How can I melt columns while keeping two together?
You could try res <- reshape(df1, idvar=c('Cond', 'Construct', 'Line', 'Plant'), varying=5:8, direction='long', sep="_") res1 <- res[res$weight!='-', -5] row.names(res1) <- NULL res1 # Cond Construct Line Plant Tube weight_shoot #1 Standard NA NA 2 199.95 #2 Cd0 IIF 43.1 1 3 51.87 #3 Cd0 IIF 43.1

Categories : R

Using sum in big dataframe (3.2GB) is very slow
try rowSums, perhaps it's faster test<-data.frame(V1=c(1,1,1,1), V2=c(2,2,2,0)) rowSums(test) I doubt however you can get faster sum functions than the vanilla sum. Another way to get the sums is the notorious apply function family apply(test, 1, sum) Did some tests and rowSums is quite fast set.seed(13) df<-data.frame(matrix(sample(c(0,1,2),500000000,T),nrow=2000)) system.time(row

Categories : R

Reversing cumulative sum to determine actuals in R
Just use diff. Assuming your dataset is called "mydf" and you want to do this for all columns, try: mydf[] <- lapply(mydf, function(x) diff(c(0, x))) mydf # X1 X2 # 1 1 5 # 2 2 4 # 3 3 3 # 4 4 2 # 5 5 1 Since diff returns a vector of lenght one less than the input, you need to pad the input with a 0 (thus also retaining the original value in that column). As @DavidArenburg men

Categories : R


Recently Add
scatter plot with facet_wrap labels
supplying user defined color in r circlize package
How to extract a date from a nc file name?
split dataframe by row number in R
Aggregate and DCast
Using ggplot2 to create dodged chart
Connecting to LinkedIn via ouath and R
Plotting pre aggregated data
image fails to appear in .Rpres
How to save multiple numbers in one cell in a matrix/dataframe?
Can ggplot change the direction of axis.ticks from downward to upward?
sapply() and ifelse() in R
Define a callback for command start
Shiny plotGoogleMaps Internet Explorer vs Chrome
Finding and labelling candidates/ouliers outside a curve in R plot
Fill a data.frame with loop, levels sets are different
How to create a heatmap in R with data frames
How to remove/eliminate outliers using IQR method
R try catch block
find row in data frame with closest match in R
How to illustrate non available data points in a different shape using ggplot2?
Recoding of data
How to look up values in R based on row and column
data layout for faceting in ggplot
copy values of a column into another column based on a condition using a loop
Acyclic graph - Smallest weight of each path between vertices
Performing a weighted distance between matrix rows with matrix operators in R
Exporting the SVM Results from R
Plot ages in a histogram using birthdays
What does %>% function mean in R?
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.