Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a table with two variables.The data is from NMR.So when I plot I get a spectrum.I found the peaks in plot.But I need to know how to list the values of peak and store them into a variable.Anyone please help.
An easy implementation based on Brian Ripley's post at R-help:
peaks <- function(x, halfWindowSize) {
windowSize <- halfWindowSize * 2 + 1
windows <- embed(x, windowSize)
localMaxima <- max.col(windows, "first") == halfWindowSize + 1
return(c(rep(FALSE, halfWindowSize), localMaxima, rep(FALSE, halfWindowSize)))
}
Example:
x <- c(1,3,1,3,1)
peaks(x, 1)
## [1] FALSE TRUE FALSE TRUE FALSE
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I would like to know if there is library like lpSolve for solving that kind of problems, I do not know if this library allows to include in the constraints the product of the two variables or how I can specify "the product is a maximum".
Defining a projection onto the feasible region allows us to use unconstrained optimization to optimize this:
tot <- 10
proj <- function(x) tot * x / sum(x)
res <- optim(1:2, function(x) -prod(proj(x)))
res$convergence
## [1] 0
-res$value
## [1] 25
proj(res$par)
## [1] 4.999799 5.000201
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to write a function that rearranges a vector in ascending or descending order. I know I can use sort and order functions but I want to do it manually.
If you want to practice writing your own sorting function, here is a example which applies a recursion approach:
mysort <- function(v, descending = F) {
if (length(v)==1) return(v)
if (descending) return(c(max(v),mysort(v[-which.max(v)],descending = descending)))
return(c(min(v),mysort(v[-which.min(v)])))
}
EXAMPLE
v <- c(1,2,5,4,2,7)
# ascending manner
mysort(v)
# descending manner
mysort(v,descending = T)
such that
> mysort(v)
[1] 1 2 2 4 5 7
> mysort(v,descending = T)
[1] 7 5 4 2 2 1
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
set.seed(1234)
dataPartition <- sample(2,nrow(data),replace=TRUE,prob=c(0.7,0.3))
trainData <- data[dataPartition ==1,]
testData <- [dataPartition ==2,]
It partition your data into two groups.
sample(2,nrow(data),replace=TRUE,prob=c(0.7,0.3))
You sample a vector in the length of your matrix which is composed of 1 and 2 with probability of 0.7 and 0.3.
trainData <- data[dataPartition ==1,]
testData <- data[Partition ==2,] ## Fixed the brackets
This is just to divide your data into two in order to be able (i presume) validate a model.
Here is a more detailed answer to why divide your data into train and test
https://stats.stackexchange.com/questions/19048/what-is-the-difference-between-test-set-and-validation-set
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to generate a plot looking like this:
Could you give me a hint how top achieve that? I startet with:
T1 <- c(23.2,34.5,76.3,65.8,12.6)
T2 <- c(15.6,12.4,21.8,20,5.2)
T3 <- c(15.6,12.4,21.8,20,5.2)
A <- gl(5,1,5,labels=c("Mähen","Wenden","Schwaden","Pressen","Abtransport"))
data <- cbind(T1,T2,T3)
rownames(data) <- levels(A)
barplot(x1,names.arg=levels(A))
barplot(x3,names.arg=levels(A))
#barplot(t(data),beside=F, ylim=c(0,100),legend.text=colnames(data),
barplot(t(data),beside=F, legend.text=colnames(data),
col=c("grey50","grey80"),ylab="Arbeitszeitbedarf [h/ha]")
This is somewhat like you requested... except for all the missing values that you do not provide.
png('rplot2.png'); par(mar=c(5,4,4,5) )
data <- cbind('T1 - Grundzeit'=T1,'T2 - Hilfszeit'=T2)
rownames(data) <- levels(A)
barplot(t(data),beside=F, legend.text=colnames(data),
col=c("grey50","grey80"),ylab="Arbeitszeitbedarf [h/ha]",
args.legend=list(inset=4,x=7,y=70))
dev.off()
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
does anyone of you knows a way to use R so to calculate and display the characteristic polynomial of the eigenvalues of a generic matrix composed by chr elements?
say i.e.
m <- matrix(c('a','b','c','d','e','f','g','h','i','l','m','n'),4,4)
Please consider that I have to apply this method to a very large matrix
Thank you in advance
You can do this with the Ryacas package, but you'll have to jump through the necessary hoops to install Yacas on your system first.
library("Ryacas")
m <- matrix(letters[1:16],4,byrow=TRUE)
yrow <- function(x) paste0("{",paste(x,collapse=","),"}")
yrow(m[1,]) ## "{a,b,c,d}"
ymat <- function(x) yrow(apply(x,1,yrow))
cheqstr <- function(x) {
paste0("Expand(CharacteristicEquation(",
ymat(x),",x),x)")
}
yacas(cheqstr(m))
## (a-x)*(f-x)*(k-x)*(p-x)-(a-x)*(f-x)*l*o+(a-x)*h*j*o-d*e*j*o-
## (a-x)*g*j*(p-x)+(a-x)*g*l*n-(a-x)*h*(k-x)*n+d*e*(k-x)*n+c*e*j*(p-x)-
## c*e*l*n+c*h*i*n-d*g*i*n-b*e*(k-x)*(p-x)+b*e*l*o-b*h*i*o+d*(f-x)*i*o+
## b*g*i*(p-x)-b*g*l*m+b*h*(k-x)*m-d*(f-x)*(k-x)*m-c*(f-x)*i*(p-x)+
## c*(f-x)*l*m-c*h*j*m+d*g*j*m;