Given I have 4 different values
intensities <- c(0.1,-0.1,0.05,-0.05)
My goal is to randomly sample every value 5 times but positive and negative values should alternate, e.g.
resultingList = (0.1, -0.05, 0.05, -0.05, 0.1, -0.1, ...)
Does anybody know an elegant way to do this in R?
Maybe something like this
# seed
set.seed(123)
plus <- rep(intensities[intensities >= 0], each = 5)
minus <- rep(intensities[intensities < 0], each = 5)
out <- numeric(length(plus) + length(minus))
out[seq(1, length(out), 2)] <- sample(plus)
out[seq(2, length(out), 2)] <- sample(minus)
out
# [1] 0.10 -0.05 0.05 -0.10 0.10 -0.05 0.05 -0.05 0.05 -0.10 0.10 -0.05 0.05 -0.05 0.05 -0.10
# [17] 0.10 -0.10 0.10 -0.10
If your list of intensities that you are sampling from come in +/- pairs, you could just sample from the list of positive values then change the sign of every other number drawn:
N <- 5
positiveIntensities <- c(0.1, 0.05)
resultingList <- sample(positiveIntensities,N,replace = T) * (-1)^(0:(N-1))
It's my solution, which creates a custom function and the argument n means the length of output. In addition, ceiling() and floor() can decide the lengths of odd and even positions.
mySample <- function(x, n){
res <- c()
res[seq(1, n, 2)] <- sample(x[x >= 0], ceiling(n / 2), T)
res[seq(2, n, 2)] <- sample(x[x < 0], floor(n / 2), T)
return(res)
}
intensities <- c(0.1, -0.1, 0.05, -0.05)
mySample(intensities, 10)
# [1] 0.10 -0.10 0.05 -0.05 0.10 -0.05 0.05 -0.05 0.05 -0.10
Related
Suppose we have the following list structure:
bla <- list(lda = list(list(auc1 = 0.85, auc2 = 0.56), list(auc1 = 0.65, auc2 = 0.72)),
j48 = list(list(auc1 = 0.99, auc2 = 0.81), list(auc1 = 0.61, auc2 = 0.85)),
c50 = list(list(auc1 = 0.92, auc2 = 0.59), list(auc1 = 0.68, auc2 = 0.80)))
The desired output is a data frame structured as:
auc1 auc2
lda 0.85 0.56
lda 0.65 0.72
j48 0.99 0.81
j48 0.61 0.85
c50 0.92 0.59
c50 0.68 0.80
My attempt is pasted below. I'm able to purrr each inner list separately using the call:
bla[[1]] %>%
map(., function(x) c(auc1 = x[["auc1"]],
auc2 = x[["auc2"]])) %>%
map_dfr(., as.list)
Any idea is appreciated.
Surprisingly only using bind_rows gives you what you want.
dplyr::bind_rows(bla)
This returns a tibble and tibbles don't have rownames.
You can do this in base R using do.call + rbind.
do.call(rbind, unlist(bla, recursive = FALSE))
# auc1 auc2
#lda1 0.85 0.56
#lda2 0.65 0.72
#j481 0.99 0.81
#j482 0.61 0.85
#c501 0.92 0.59
#c502 0.68 0.8
We can also use rbindlist
library(data.table)
rbindlist(bla)
I'm trying to substitute some characters by some strings, but when I try this happens:
Group <- "ABC"
A <- "0.25 0.65 0.48"
B <- "0.054 0.41 0.09"
C <- "0.8 0.047 0.34"
Group <- gsub("A", A, Group)
Group <- gsub("B", B, Group)
Group <- gsub("C", C, Group)
Group
When I group them there is no space between A, B and C. The above code results in:
0.25 0.65 0.480.054 0.41 0.090.8 0.047 0.34
I want that the input be like this:
0.25 0.65 0.48 0.054 0.41 0.09 0.8 0.047 0.34
I will appreciate if you can help me with this.
There are several syntactical errors, but let me present you what I think you are trying to accomplish:
Group <- 'ABC'
A <- paste(0.25, 0.65, 0.48)
Group = gsub('A', A, Group)
[1] "0.25 0.65 0.48BC"
EDIT: Seeing your reformatted question, I would say the only change is to put a space between your Group letters:
Group <- 'A B C'
Or paste an empty character at the end of all groups of numbers:
A <- paste(0.25, 0.65, 0.48, "")
You can transform Group a bit, i.e., trimsw(gsub(""," ",Group)), then " " is inserted among characters in Group.
just use paste with collapse = "":
A <- "0.25 0.65 0.48"
B <- "0.054 0.41 0.09"
C <- "0.8 0.047 0.34"
paste(A, B, C, collaspe = "")
"0.25 0.65 0.48 0.054 0.41 0.09 0.8 0.047 0.34 "
I'm using the psych package for factor analysis. I want to specify the labels of the latent factors, either in the fa() object, or when graphing with fa.diagram().
For example, with toy data:
require(psych)
n <- 100
choices <- 1:5
df <- data.frame(a=sample(choices, replace=TRUE, size=n),
b=sample(choices, replace=TRUE, size=n),
c=sample(choices, replace=TRUE, size=n),
d=sample(choices, replace=TRUE, size=n))
model <- fa(df, nfactors=2, fm="pa", rotate="promax")
model
Factor Analysis using method = pa
Call: fa(r = df, nfactors = 2, rotate = "promax", fm = "pa")
Standardized loadings (pattern matrix) based upon correlation matrix
PA1 PA2 h2 u2 com
a 0.45 -0.49 0.47 0.53 2.0
b 0.22 0.36 0.17 0.83 1.6
c -0.02 0.20 0.04 0.96 1.0
d 0.66 0.07 0.43 0.57 1.0
I want to change PA1 and PA2 to FactorA and FactorB, either by changing the model object itself, or adjusting the labels in the output of fa.diagram():
The docs for fa.diagram have a labels argument, but no examples, and the experimentation I've done so far hasn't been fruitful. Any help much appreciated!
With str(model) I found the $loadings attribute, which fa.diagram() uses to render the diagram. Modifying colnames() of model$loadings did the trick.
colnames(model$loadings) <- c("FactorA", "FactorB")
fa.diagram(model)
What would be the simplest way to round up a value to a specific significant figure?
Something like the function signif(), but only perform rounding up (not down)
For example: Round up 0.001145288 to 1 significant figure would yield 0.002
Any suggestions will be appreciated :)
Cheers!
This function provides the desired output in the case of one significant figure:
upround1 <- function(x) {
if (isTRUE(all.equal(x,0))) return (x)
decs <- 10^floor(log10(abs(x)))
ceiling(x/decs)*decs
}
Examples:
upround1(0.001145288)
#[1] 0.002
upround1(0.0008145258)
#[1] 9e-04
upround1(11)
#[1] 20
upround1(-11)
#[1] -10
upround1(-0.023)
#[1] -0.02
I created a function which changes the format to scientific, splits the string by 'e' and uses the information to ceiling the number.
my_ceiling <- function(x){
num_string <- format(x, scientific=TRUE)
n <- strsplit(num_string, "e")
n1 <- sapply(n, function(x) as.numeric(x[1]))
n2 <- sapply(n, function(x) as.numeric(x[2]))
ceiling(n1) * 10^(n2)
}
my_ceiling(0.001145288)
# [1] 0.002
my_ceiling(0.0974343)
# [1] 0.1
my_ceiling(0)
# [1] 0
set.seed(1)
x <- runif(10, 0.001, 0.01)
my_ceiling(x)
# [1] 0.004 0.005 0.007 0.010 0.003 0.010 0.010 0.007
# [9] 0.007 0.002
I have a dataset called volcano that looks like this:
DiffMean P.value
-0.0246757556 0.1
0.0050993889 0.002
-0.0169992614 0.008
0.0039905857 0.03
-0.0081568420 0.02
-0.0279989935 0.03
0.0313951281 0.44
-0.0097932018 0.22
-0.1033745673 0.003
0.1143251388 0.02
-0.0738617112 0.004
-0.0011579184 0.1
-0.0008561962 0.022
0.0435398270 0.11
-0.0380242369 0.05
0.1533720177 0.03
I want to plot this using ggplot, but I want the colors to be red if DiffMean < 0 and P.value < 0.05 or blue if DiffMean > 0 and P.value < 0.05.
What I have so far is:
volcano$threshold = as.factor(abs(volcano$DiffMean)>0 & volcano$p.value.adj< 0.05)
ggplot(data=volcano, aes(x=DiffMean, y=-1*log10(p.value), colour=threshold)) +
geom_point(aes(alpha=0.4, size=1.75)) +
xlim(c(-1,1)) + ylim(c(0,25))
But I don't know how to use this two thresholds.
I would do something like this :
volcano$threshold <-
factor(ifelse(volcano$DiffMean>0 & volcano$p.value< 0.05,
1,
ifelse(volcano$DiffMean<0 & volcano$p.value< 0.05,
-1,
0)
))
library(ggplot2)
ggplot(data=volcano, aes(x=DiffMean, y=-1*log10(p.value), colour=threshold)) +
geom_point(alpha=0.4, size=5) +
scale_y_log10()