w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
  Home » R » Page 1
Loops, dataframes and ggplot
Below you find an alternative: using the melt function from reshape2 and then faceting with facet_wrap. require(ggplot2) require(reshape2) data.melt <- melt(rawdata1, id.var='Period') ggplot(data.melt, aes(Period, value)) + geom_line() + facet_wrap(~variable, scales='free_y') If you want to use multiplot instead, you could do the following: out <- lapply(names(rawdata1)[-1],

Categories : R

R install package RevoScaleR
I am not sure what R version you are currently using but the latest stable version is 3.1.2. If the package installation warning is telling that the package you're trying to install is not available, that basically means that the package developer has not yet made a version of the package that is compatible with the R version you're currently running. If you really need to use this package (if y

Categories : R

Simple lapply task
You can use sapply to get a vector > sapply(dir, function(x) paste("year", x, sep="_")) [1] "year_2013" "year_2012" "year_2011" Use lapply to get a list > lapply(dir, function(x) paste("year", x, sep="_")) [[1]] [1] "year_2013" [[2]] [1] "year_2012" [[3]] [1] "year_2011"

Categories : R

R, data.table, group by column *numbers* AND sum a column
Here's how I would approach this: f <- function(columns) { Get <- if (!is.numeric(columns)) match(columns, names(DT)) else columns columns <- names(DT)[Get] DT[, sum(N), by = columns] } The first line (Get..) keeps "columns" as numeric if it's already numeric or it converts it from characters to numeric if they are not. Test it out with some sample data: set.seed(1) DT <-

Categories : R

R: Creating colorized barplot from segments()
You can use rect for this: lapply(ind, function(x) { plot(unlist(x[, c(3, 5)]), unlist(x[, c(4, 6)]), type='n', xlab='Time [Years]', ylab='Value [mm]', main=x[1, 1]) apply(x, 1, function(y) { rect(y[3], min(y[4], 0), y[5], max(y[4], 0), col=if(as.numeric(y[4]) < 0) 'red' else 'blue') abline(h=0) }) })

Categories : R

How do I create 3 or four classes of diameter from a range of diameters in R?
As @Vincent Guillemot notes, all you need is ?cut. my.data <- read.table(text="LOGID DIAMETER 1 10 2 30 3 20 4 10 5 8 6 5", header=TRUE) my.data$CLASS <- cut(my.data$DIAMETER, breaks=c(-Inf, 10, 20, 30, Inf), labels=c(1,2,3,4)) my.data # LOGID DIAMETER CLASS # 1 1 10 1 # 2 2 30 3 # 3 3

Categories : R

How to make this graph in R
this chart is called a 'Sankey diagram'. I don't really know how to do in R but I am using d3 to create this at work. An example here: http://bost.ocks.org/mike/sankey/

Categories : R

RStudio Shiny dynamic selectize
You need to add the css dynamically also. To target the selectize input you need to target select#dataset + .selectize-control rather then select#dataset library(shiny) runApp(list( ui = bootstrapPage( radioButtons("viz_multiple", "Select Type:", c("Select From List (can use Up/Down + Enter)" = "multiple", "Search One (Delete then type keyword)" = "sing

Categories : R

strptime not recognizing %b/%B?
I am a bit confused by R's behaviour in the case of missing values in strptime. The documentation is clear enough: For strptime the input string need not specify the date completely: it is assumed that unspecified seconds, minutes or hours are zero, and an unspecified year, month or day is the current one. Some components may be returned as NA (but an unknown tzone component is represent

Categories : R

constrained nonlinear minimization with many variables
Try this: co <- coef(lm.fit(ui, ci)) co[is.na(co)] <- 0 res <- nloptr( x0=co, eval_f=objective, eval_g_eq = constraints, opts=list(algorithm = "NLOPT_LD_SLSQP")) giving: > res Call: nloptr(x0 = co, eval_f = objective, eval_g_eq = constraints, opts = list(algorithm = "NLOPT_LD_SLSQP")) Minimization using NLopt version 2.4.0 NLopt solver status: 4 ( NLOPT_XTOL_REACHED: O

Categories : R

removing elements of the environment using a loop
for(i in 1:16) { rm(list=paste0('Factor',i)) } although rm(list=paste0('Factor',1:16)) or rm(list=ls(pattern="Factor"))would be more appropriate...

Categories : R

Optimize the performance of a function
A couple thoughts: If you have a gigundo dataset, well, it's going to take time; or you'll need to learn to use the parallel package. I don't think you want to re-define newdata$ALTMISTA every time thru the loop, as you're just overwriting the values. You could remove the i loop by using a vectorized operation with ifelse : set.seed(1) foo<-sample(c(-1,1),10,rep=T) foo [1] -1 -1 1

Categories : R

density histogram by intervals in R
If I understand you right, you have pre-binned data: interval count [ 0, 10) 1 [ 10, 30) 15 [ 30, 200) 44 [200, 400) 40 total 100 Your code does not work, because hist tries to the bin the values in x by itself. 1 goes to the first interval, 15 to the second and 44 and 40 to the third one. I don't know how to do this with hist. With ggplot2, you can u

Categories : R

How to give a "/" in a column name to a dataframe in R?
You can use check.names=FALSE in the data.frame. By default, it is TRUE. And when it is TRUE, the function make.names changes the colnames. ie. make.names('Cost/Day') #[1] "Cost.Day" So, try dat <- data.frame("Cost/Day"=1:10,"Days"=11:20, check.names=FALSE) head(dat,2) # Cost/Day Days #1 1 11 #2 2 12 The specific lines in data.frame function changing the

Categories : R

Define a number line in R
I guess you mean (-1)^n -1/n ? Because -n -1/n would just give you an infimum of -infinity. Although for my answer it does not matter. To make a line up to given n.lim, I would do: nlim<-10000 n <- 1:nlim myline <- (-1)^n-1/n Which gives for the first 15 elements: myline[1:15] [1] -2.0000000 0.5000000 -1.3333333 0.7500000 -1.2000000 0.8333333 [7] -1.1428571 0.8750000

Categories : R

Tabular data to matrix in R
I would suggest to change from letters to numbers (which is straight forward using the ASCII codes) and then use the linearized indices of R matrices to access each pair in a vectorwise manner. Minimal example: N <- 3 d <- data.frame(x = c(1,2), y = c(2,3), v = c(0.1, 0.2)) m <- matrix(0, N, N) m[(d$y-1)*N+d$x] = d$v The output is: [,1] [,2] [,3] [1,] 0 0.1 0.0 [2,] 0

Categories : R

List all one-digit differences from a vector
You could try the following: a <- c(0, 0, 0, 0, 1) # your input vector m <- expand.grid(rep(list(0:1), length(a)) # all combinations of 0/1 of length a temp <- sapply(seq_along(a), function(i) m[,i] == a[i]) # check the differences m[rowSums(temp) == (length(a)-1),] # use the index to subset # Var1 Var2 Var3 Var4 Var5 #1 0 0 0 0 0 #18 1 0 0 0 1 #19

Categories : R

Including Rnw files within a package
To answer this question: Include the .Rnw files in ./pkgname/inst/latex then when you build the package, the ./latex folder will go to the root level of the package. You can then extract the .Rnw files using system.file("latex", "mytemplate.Rnw", package = "pkgname").

Categories : R

ggplot2: How to plot an orthogonal regression line?
Caveat: not familiar with this method I think you should be able to just pass the slope and intercept to geom_abline to produce the fitted line. Alternatively, you could define your own method to pass to stat_smooth (as shown at the link smooth.Pspline wrapper for stat_smooth (in ggplot2)). I used the Deming function from the MethComp package as suggested at link How to calculate Total least squa

Categories : R

Spatial interpolation in R gstat without the message
Various ways exist of stopping output - the nicest being if the function has an option to suppress it. But krige doesn't seem to have that. capture.output works here: > rm(zn.tr1) > zn.tr1 # there is no zn.tr1 Error: object 'zn.tr1' not found > z = capture.output(zn.tr1 <- krige(log(zinc) ~ x + y , meuse, meuse.grid)) > str(zn.tr1) # there is now Formal class 'SpatialPixelsDataFr

Categories : R

How to draw a graph for erdos.renyi.game method in R
For a line plot you need to use: plot(d,type="l") The default for the plot() function is the dotplot. If all you want is to get a graph of the degree distribution, you can use hist(degree(graph)). The function degree.distribution() gives you a vector of the relative frequencies of degrees. You can get a histogram showing the distribution of degree values by calling degree() and feeding it int

Categories : R

Sorting by a slice of the text string
Assuming chrPos is in ColumnA, please try in a helper column: =IF(FIND("_",A1)=5,CHAR(64+MID(A1,4,1)),CHAR(64+MID(A1,4,2)))&REPT("0",8-LEN(A1)+FIND("_",A1))&MID(A1,FIND("_",A1)+1,8) OR, for additional requirements as mentioned in comments: =IF(MID(A1,4,1)="M","W",IF(MID(A1,4,1)="X","X",IF(MID(A1,4,1)="Y","Y",IF(FIND("_",A1)=5,CHAR(64+MID(A1,4,1)),CHAR(64+MID(A1,4,2))))))&REPT(

Categories : R

igraph plot graph with vertex size dependent on betweenness centrality
Using igraph, and following the example in ?betweenness: g <- random.graph.game(10, 3/10) plot(g, vertex.size=betweenness(g)) (note numbers are node numbers, not betweenness value) You may want to rescale your vertex size if you have lots of large values or otherwise improve the visualisation. g = graph.lattice(c(10,4)) plot(g,vertex.size=betweenness(g)/10) without the /10 the vertic

Categories : R

Error in executing knit HTML in R Studio
When you run "Knit HTML", the code is trying to find the file you're reading in the same directory where .Rmd is located. As far as I see you have two options. Try specifying the absolute path to the file (not very robust, but handy in some cases). Figure out the relative path to the file. If you have your .Rmd file in / and data in /data, relative path should be, e.g., read.table("./data/myfile

Categories : R

R Graphics Legends Commands
You can use expression plot(0) legend(x = "topleft", legend = expression(alpha[a], mu[b], gamma[c]))

Categories : R

How to get chunk name in knitr?
I think this solution is imperfect because it requires a bit of care in making sure the correct chunks are evaluated, but it gets around the problem that chunk options are evaluated before hooks are called. In short, it doesn't use a hook, but instead uses the fact that chunk options can be R expressions. In this case, a function e() is used that relies on a global counter variable to dictate whet

Categories : R

How eliminate codes with format 00.00.00A
You first have to generate the codes : codes<-sapply(180001:200000, function(x){ decomp<-unlist(strsplit(as.character(x),"")) y<-paste(paste(decomp[seq(1,6,2)],decomp[seq(2,6,2)],sep=""),collapse=".") return(y)}) Then you can do : toMatch<-paste(rep(codes,e=3),c('U','B','R'),sep="") toMatch[1:5] [1] "18.00.01U" "18.00.01

Categories : R

Extract data for all days for last 30 days from R data frame
You don't need an iteration. What you could do is, assuming your data.frame is called X, and the date column, DATE, you could write: X$DATE=as.Date(X$DATE, format='%d-%B-%Y') the 'format' argument is to match your date format you specify in you question. Then, to get the lines you are interested in, something like: X[X$DATE>=as.Date(today(),format='%d-%B-%Y')-30)] which is all the lines

Categories : R

Error in sorting column of a data frame
I'm guessing data[[1]] is only one element, not a column or row and that might explain why your sorting is kind of funny. You can try : data<-data[order(data[,2],data[,1]),]

Categories : R

R (and R Studio) ignoring Environment variables (default package library)
The best solution is to not use "User Variables", but "System Variables", as the latter are profile-independant. Changing "User Variables" for a given profile seem to require administrator rights for that profile (at least in our network setup).

Categories : R

vectorize stringr str_match to remove a for loop
You have to use [,2] instead of [2] because the output is a 2 column matrix and by indexing [2], you are getting only the 2nd element i.e. "http://www.slate.fr/story/" instead of the 2nd column`. str_match(url, "http://.*slate.fr/(.*?)/")[,2] #[1] "france" "story" "grand-format" From the description of ?str_match Vectorised over ‘string’. ‘pattern’ should be a single p

Categories : R

how to write multiple dataframe to multiple sheet of one csv excel file in R?
This is not possible. That is the functionality of csv to be just in one sheet so that you can view it either from notepad or any other such software. If you still try to write it would get over ridden. Just try to open a csv and open a new sheet and just write some values and save it. The values which were already there is erased. one excel file in csv format can have only one sheet.

Categories : R

Clustering connected set of points (longitude,latitude) using R
Maybe this. First make some coordinates: > x=c(1.000000, 1.055672, 1.038712, 1.094459, 1.133179, 1.116241, 1.126053, 1.181824 ,1.377892, 5.869881, 5.925270) > y=c(1.333368, 1.304790, 1.347332, 1.318743, 1.332676, 1.375229, 1.572287, 1.544174, 2.371105 ,2.337032, 2.383415) Make into a data frame > xy = data.frame(x=x,y=y) Now use outer to loop over all pairs of rows and columns to co

Categories : R

Replicate a bunch of syntax (multiple lines) to 20 times with various objects
Something like this: #dummy list mylist <- list(a=data.frame(x=runif(10)), b=data.frame(x=runif(10)), c=data.frame(x=runif(10))) #make temporary Rmd file write.table( paste0( "```{r summary_table",names(mylist),", results='asis'} kable(mylist['",names(mylist),"'], digits=1, align='r' ) ```"), "temp.Rmd",row.names=FALSE,col.names=FALSE,quote=FALSE) Output - temp.Rmd

Categories : R

Filling in each of the 9 areas in circle using R
You can do this with a combination of pie and draw.circle: library(plotrix) library(RColorBrewer) cols <- brewer.pal(9, 'Set3') pie(rep(1, 4), col=cols[1:4], init.angle=45, radius=1, labels='') par(new=TRUE) pie(rep(1, 4), col=cols[5:8], init.angle=45, radius=0.5, labels='') draw.circle(0, 0, 0.166, col=cols[9]) Set col as necessary to fill (or not) the individual sections, e.g.: pie(re

Categories : R

How to find the smallest number of several variables and return the column name
One simple approach would be (assuming your data called df) df[c("min", "minCol")] <- t(apply(df, 1, function(x) c(min(x), names(x[which.min(x)])))) df # A B C D min minCol # 1 23 23 12 34 12 C # 2 12 13 11 9 9 D # 3 70 80 67 76 67 C # 4 43 23 25 40 23 B Another approach (offered by @akrun) would be a combination of pmin, do.call and max.col val <- do.call

Categories : R

how to convert mtw minitab files to readable mtp files in R
If you want to convert your mtw worksheet to mtp extension, simply open the mtw file on minitab and from File menu select "Save Current Worksheet As" and save it to mtp extension. It will save it as Text not Encrypted You would be able then to use foreign

Categories : R

How do I fix encoding in a data frame regardless of its row or column in R(using dplyr)?
You could correct this in a couple of ways. Read the file with the proper encoding (UTF-8) read.csv2(file("filename.csv", encoding="UTF-8")) After reading the file, apply functions to convert to the UTF-8 encoding library(stringi) df[] <- lapply(df, function(x) stri_encode(x, "", "UTF-8"))

Categories : R

why can't get the root of equation in R function solve?
That's because the determinant of a is 0. You've got to specify the tolerance in your call : solve(a,b,tol=1e-20) [1] 0.1995899 -1.3991798 2.1995899 > a%*%c(0.1995899,-1.3991798,2.1995899) [,1] [1,] 4 [2,] 7 [3,] 10 But, of course, that is one solution among an infinity of it...

Categories : R




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.