simultaneous function use in R - r

I have 2 function in R, the first is :
cyii=function(a,b,L)
{
d=outer(a,b,`-`);I=outer(a,b,`==`)
d=d[upper.tri(d,diag=T)];I=I[upper.tri(I,diag=T)]
L[1]^2*exp(-0.25*d^2/L[2]^2) + I*L[3]^2
}
The second function called the first function many time
zii=list()
for(i in 1:(n-1))
{
zii[[i]]=cyii(v1,v1,H[c(5*i-4,5*i-3,5*n-3+i)])
}
Where v1 is any vector of numbers and H is a vector of parameters. Below is a reproducable example.
dput(v1)=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
dput(H)=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
n=3
Is there a possible way to use the apply function or any other in order to avoid using the for loop, given that for every n I need to provide the first function with diffrent values from H

Yes, shouldn't be a problem at all
# What you supplied
cyii=function(a,b,L)
{
d=outer(a,b,`-`);I=outer(a,b,`==`)
d=d[upper.tri(d,diag=T)];I=I[upper.tri(I,diag=T)]
L[1]^2*exp(-0.25*d^2/L[2]^2) + I*L[3]^2
}
v1=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
H=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
n=3
zii=list()
for(i in 1:(n-1))
{
zii[[i]]=cyii(v1,v1,H[c(5*i-4,5*i-3,5*n-3+i)])
}
# Change it up a little to use 'lapply'
N <- 1:(n-1)
z2 <- lapply(N, function(i){
cyii(v1,v1,H[c(5*i-4,5*i-3,5*n-3+i)])
})
identical(zii,z2)
#[1] TRUE

Related

How can I change the grid line spacing on a ggplot2 dotplot?

I'm analyzing data from the result of pulling 10 numbered balls from a jar with replacement, repeated 70 times. Here's my code (data included):
numbers <- c(8, 3, 9, 5, 1, 9, 10, 8, 8, 1, 9, 9, 8, 5, 1, 10, 5, 9, 6, 4, 10, 3,
10, 9, 8, 4, 8, 8, 9, 9, 1, 5, 9, 8, 4, 1, 8, 6, 7, 8, 2, 9, 5, 6,
10, 9, 1, 1, 5, 6, 2, 8, 6, 5, 2, 5, 4, 10, 10, 2, 2, 4, 9, 6, 9,
9, 6, 10, 9, 10)
num_frame <- data.frame(numbers)
ggplot(num_frame) +
geom_dotplot(aes(numbers), binwidth = 1, dotsize = 0.4) +
theme_bw() +
xlab("Numbers") +
ylab("Frequency")
The resulting plot is nice, except it labels gridlines at 0, 2.5, 5, 7.5, and 10, which is obviously not what I want. The scale is fine, but I would like the gridlines to be at integer values 1 through 10 (0 is fine too if necessary). How can I do this? I'd also like the y-axis to adjust likewise so that the grid is still square. Thanks!
Just add:
scale_x_continuous(breaks=1:10, minor_breaks=NULL)
minor_breaks=NULL suppress lines that aren't at the breaks

How do I return the intersection of two lists including duplicates in mathematica?

How do I find the intersection of two lists including duplicates in mathematica?
So, If I have this:
list1 = {1, 1, 3, 4, 5, 6, 6, 6, 7, 7, 10, 11, 11};
list2 = {1, 1, 4, 5, 5, 6, 6, 7, 7, 8, 11, 11, 13, 14};
I'd want it to return this:
IntersectionIncludingDuplicates[list1, list2] = {1, 1, 4, 5, 6, 6, 7, 7, 11, 11}
Thanks for any and all help!
Here's one way:
Catenate#KeyValueMap[ConstantArray]#
MapThread[Min, KeyIntersection[Counts /# {list1, list2}]]
Breaking it down:
Count how many times each element occurs in each list (Counts)
Retain only those elements which occur in both (KeyIntersection)
Take the smaller number of occurrences (MapThread, Min) and replicate the given element that many times (ConstantArray)
Edit : fixed and tested..
you might use regular Intersection , then Count , something like
ConstantArray[#, Min[Count[list1, #], Count[list2, #]]] & /#
Intersection[list1, list2] // Flatten
{1, 1, 4, 5, 6, 6, 7, 7, 11, 11}
in functional form generalised to take an arbitrary number of lists:
IntersectionIncludingDuplicates[lists__List] :=
ConstantArray[#,
Function[{v}, Min ## (Count[#, v] & /# {lists})]##] & /#
Intersection[lists] // Flatten
IntersectionIncludingDuplicates[list1, list2]
{1, 1, 4, 5, 6, 6, 7, 7, 11, 11}
Simple code to understand. Assumes input lists are sorted.
list1 = {1, 1, 3, 4, 5, 6, 6, 6, 7, 7, 10, 11, 11};
list2 = {1, 1, 4, 5, 5, 6, 6, 7, 7, 8, 11, 11, 13, 14};
IntersectionIncludingDuplicates[list1_, list2_] := Module[
{out = {}, i = j = 1},
While[i <= Length[list1] && j <= Length[list2],
If[list1[[i]] == list2[[j]],
AppendTo[out, list1[[i]]];
i++; j++,
If[list1[[i]] < list2[[j]], i++, j++]]];
out]
IntersectionIncludingDuplicates[list1, list2]
{1, 1, 4, 5, 6, 6, 7, 7, 11, 11}

Subsample a matrix by selection locations with specific values within a matrix in R

I'm have to use R instead of Matlab and I'm new to it.
I have a large array of data repeating like 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10...
I need to find the locations where values equal to 1, 4, 7, 10 are found to create a sample using those locations.
In this case it will be position(=corresponding value) 1(=1) 4(=4) 7(=7) 10(=10) 11(=1) 14(=4) 17(=7) 20(=10) and so on.
in MatLab it would be y=find(ismember(x,[1, 4, 7, 10 ])),
Please, help! Thanks, Pavel
something like this?
foo <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
bar <- c(1, 4, 7, 10)
which(foo %in% bar)
#> [1] 1 4 7 10 11 14 17 20
#nicola, feel free to copy my answer and get the recognition for your answer, simply trying to close answered questions.
The %in% operator is what you want. For example,
# data in x
targets <- c(1, 4, 7, 10)
locations <- x %in% targets
# locations is a logical vector you can then use:
y <- x[locations]
There'll be an extra step or two if you wanted the row and column indices of the locations, but it's not clear if you do. (Note, the logicals will be in column order).

Data Subsetting: Lists within list in R

I have a list containing 100 lists within it, each of which has 552 numerical values. How do I sequentially extract the 1st value (and so on up to 552) from each of the 100 lists?
Example: 5 lists within a list containing the numbers 1-10
list(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), c(1, 2, 3, 4, 5, 6, 7,
8, 9, 10), c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), c(1, 2, 3, 4, 5,
6, 7, 8, 9, 10), c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
I want to extract each term sequentially i.e. 1,1,1,1,1 and then
2,2,2,2,2 and so on
This statement produces a list of vectors, taking the first element of each of your original vectors, the second element, etc., giving NA for the value of a short vector:
num <- max(unlist(lapply(x, length))) ## Length of the longest vector in x
lapply(seq(num), function(i) unlist(lapply(x, `[`, i)))
And here's a matrix approach:
matrix(unlist(x), ncol=length(x))
The rows of that matrix are your elements. This relies on each vector being the same length.

How to create a cuvilinear line segment with loess and lines using R

I am trying to fit a curved line segment to a dataset. While I can create the line it is always connected back to the starting point. I can't figure out how to get rid of this. I would really appreciate any help. Here is the code
mscF25=c(-12.94382785, -11.0281518, -9.186403952, -7.691576905, -6.470229134, -5.43000796, -4.559074508, -12.87271022, -10.0646268, -6.796208225, -4.433351598, -2.928135666, -1.979265556, -1.38936463, -11.05819006, -7.785838826, -5.297330858, -3.674159165, -2.64702678, -1.980973252, -1.533714976, -11.83971039, -9.168353808, -6.89192172, -5.23424594, -4.033326594, -3.148798626, -2.480469911)
bscF25=c(4, 5, 6, 7, 8, 9, 10, 4, 5, 6, 7, 8, 9, 10, 4, 5, 6, 7, 8, 9, 10, 4, 5, 6, 7, 8, 9, 10)
df25 <- data.frame(bscF25,mscF25)
plot(mscF25 ~ bscF25, data = df25)
ls25 <- loess(mscF25 ~ bscF25, data = df25, span = 3)
lines(df25$bscF25, ls25$fitted)
You might try the scatter.smooth function: "Plot and add a smooth curve computed by loess to a scatter plot"
scatter.smooth(x = df25$bscF25, y = df25$mscF25, span = 3)

Resources