Finding shift/phase between two datasets [closed] - r

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 have a large dataset D with (x,y) coordinates in 2D. I have almost the same (with a very few elements missing) dataset D' but with a constant shift, s. That is, the elements of D' are (x+s, y). How do I compute the shift, in an efficient way? Thanks. An r code would be terrific.

If the values of y are equal in D and D' you can perform a join on y and a rolling join on x with data.table.
library(data.table)
set.seed(1)
D <- data.frame(x = runif(100,1,100), y = runif(100,1,100))
Dprime <- D[sample(1:100,90),]
Dprime$x <- Dprime$x + runif(length(Dprime$x),2.8,3.2)
setDT(Dprime)
setDT(D)
D[,x.original := x]
Dprime[,x.shift := x]
Dprime[D,on=c("y","x"),roll = "nearest"][,.(Shift = x.shift - x.original)][,median(Shift,na.rm=TRUE)]
#[1] 2.997595
This addresses the issue of potential duplicate values of y. Those values which are missing in D' simply get NA and are eliminated by median(x,na.rm=TRUE).
For more options on roll = that may be better suited to your unique problem, see the roll section of help(data.table).

Every x value in D' provides an estimate of s s = x' - x so probably your best estimate of s is the average of those.
s_est = mean(D'$x - D$x, na.rm = T)

Related

Finding two middle values in R? [closed]

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 1 year ago.
Improve this question
EDIT: the output is : [1] 49 65. Only allowed to use INDEXING. Code cannot be hard coded.
Can anyone please provide the code in R for finding two middle values in a vector with 10 elements?
The code would need to work for any vector size. so it cannot be hardcoded
The elements are 59,46,76,60,49,65,82,68,99,52
x <- c(59,46,76,60,49,65,82,68,99,52)
if(length(x)%%2 == 0) {
x[c(length(x)/2,(length(x)/2+1))]
} else{
x[ceiling(length(x)/2)]
}
[1] 49 65
Lets create a vector array with our values
array <- c(59,46,76,60,49,65,82,68,99,52)
From left to right, the middle two values of this array are the 5th and 6th
array[5:6]
# 49 65
If we want the numerically 5th and 6th highest number we can go with
sort(array)[5:6]
# 60 65
If you want to know which position of the original array the middle two elements are in, you can do
which(array %in% sort(array)[5:6])
# 4 6
You can expand the above programmatically for any array of even length by doing
n <- length(array)
x <- n/2
y <- x + 1
Then in any of the previous three examples, just replace 5 by x and 6 by y.
Like this ?
x <- c(59,46,76,60,49,65,82,68,99,52)
c(x[length(x)/2],x[length(x)/2 + 1])

Matrix solving in R with for loop [closed]

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'm trying to solve matrix in R.I have 3 matrix like this, Uf[11,3], Uft[3,11] and AT[3,3] .For the first two 11 rows and columns represent 11 different values that I want to use. And at last I need to reach 11 different "D"
values.D = (11/3) * Uft * AT * Uf
but if i try this with for loop I had [11,11] matrix .So here is the problem , how can I get [11,1] or [1,11] matrix to see my D results. I also get this error "subscript out of bounds" when I try this code.
for (i in 1:11) {
print((1/3) * 11 * Uft[[,i]] %*% Uf[[i, ]] %*% AT[[,]])
}
There should be 11 ui vectors to calculates "D" datas.But i have (3,11) matrix instead of 11 ui vectors.
***Here is my Ut matrix(combination of ui).Uft is transpose of Uf.
Now i want to use each row of Uft and each column of Uf and all of AT to calcutes 11 different "D".
I think you have several syntax errors to be checked:
You should use [] rather than [[]] for matrix indexing
drop = FALSE should be set
For example
for (i in 1:11) {
print(11 / 3 * Uft[, i, drop = FALSE] %*% Uf[i, , drop = FALSE] %*% AT)
}

I have to write some code but I’m completely stuck, any help would be amazing [closed]

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 2 years ago.
Improve this question
I have to create a code or function in R that displays all the even numbers from 6:500 and beside each one any 2 prime numbers which can be added together to get said even number. I am really struggling with this and so far all I have got is a vector of the even numbers and a vector of the prime numbers. I have some idea of what I have to do but I have not clue how to code it! Any help would be greatly appreciated!
Ok, so I'm sure there's a more efficient way to do it, but you're probably going to want to nest a loop inside a loop. I'm going to use the library primes to generate my prime numbers because I'm lazy and it has a built in variable primes that contains the first 1000 primes.
library(primes)
#generate the even numbers from 6 to 500
evens <- 3:250 * 2
#generate the prime numbers less than 500
p <- primes[primes < 500]
But you said you already had that, but now I'm going to start making a data frame because I think better in data frames:
#The name of variables 'n' and 'x' are arbitrary
df <- data.frame("n" = evens,"x" = 0)
The 0 in column x is just a placeholder that I plan to fill in with my loop.
#add the library tidyverse because it contains the %in% notation that you'll want
library(tidyverse)
#start the loop and have it run from the first row to the last
for(i in 1:nrow(df)) {
#make a temp value n that will be used in the next loop
n <- df$n[i]
#start a second loop that runs through each p such that p < n
for(j in p[p < n]) {
#Take the j's and subtract them from n.
#If they are in p, then we have 2 primes that add to the right number
if((n - j) %in% p) {
#If we found one prime, them we assign it to that placeholder in x and break the loop
df$x[i] <- j
break
}
}
}
#Now that we're done, we can just add a new column that is the difference of the previous two.
df$y <- df$n - df$x

Bidirectional assignment operator in R [closed]

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 4 years ago.
Improve this question
Is there an R function that assigns variables bidirectionally? For example, let <-> represent a bidirectional assignment operator.
a <-> b
a
> b
b
> a
One can define something like:
`%<->%` <- function(x,y){
t <- y
assign(deparse(substitute(y)), x, envir=parent.frame())
assign(deparse(substitute(x)), t, envir=parent.frame())
}
a <- 1
b <- 2
a %<->% b
a
[1] 2
b
[1] 1
Such an operator would not make sense with the way R functions:
From Hadley Wickham's book Advanced R, section "Binding basics":
Consider this code:
x <- c(1, 2, 3)
[...] this code is doing two things:
It’s creating an object, a vector of values, c(1, 2, 3).
And it’s binding that object to a name, x.
So, for instance, when you run:
a <- 1
you are creating a numerical vector with one element and you are binding it to the name a.
a <-> b
would be binding names to one another, which makes no sense in R.
Also note than when you do:
a <- 1
b <- a
b
# [1] 1
You get 1 as the output, not a, because you create another binding (b) to the numerical vector with the value 1. And when you run b, the output is the object binding to it (1), not another name this object is binding to.
Note: Hadley explains all this very clearly with diagrams in his book.

Use R to translate 'coded' table [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a problem with using R to 'translate' back a coded table. So I have a table with table elements consisting of XX,XY,YY. I have a second table (.csv) with the proper meaning of the X and Y - so it might look like, if X=1 and Y=2,
XY is transformed into 12
XX is transformed into 11 ...
can anybody hint at a good starting point to write such a program/ piece of code in R?
This is slightly different than a lookup table in that you're actually regexing and replacing parts of each element. The qdap (Quantitative Discourse Analysis Package) has a mgsub (multiple gsub) function that can handle this easily.
library(qdap)
#recreate scenerio with quick character vector (no need for quotes)
z <- factor(qcv(XX,XY,YY))
#replace all X and Ys with 1 and 2
mgsub(pattern = c("X", "Y"), replacement = c(1, 2), text.var = z)
#Even better if you have the code book read in, say it looks like this:
code.book <- data.frame(symb = c("X", "Y"), replacement = c(1, 2))
# > code.book
# symb replacement
# 1 X 1
# 2 Y 2
mgsub(code.book$symb, code.book$replacement, z)

Resources