Can someone help with the nuance in the wilcox.test function in R? This is from a homework assignment for my grad stats class. First I generate a dataset in R:
       set.seed(13)
       d2 <- cbind(rnorm(11, 142, 21), rnorm(11, 123, 23))

Then perform a wilcox test on the data:
        wilcox.test(d2, alternative="two.sided", conf.level = 0.95)

Then the resulting test statistic and P value are 253 and 4.78x10^-7 respectively
However, when I split up the data into two separate vectors and run a wilcoxon test specifying that the data is paired, I get very different results:
        d2A<- d2[,1] ; d2B<- d2[,2]
        wilcox.test(d2A, d2B, paired=TRUE, alternative="two.sided", conf.level = 0.95)

The resulting test statistic and P value are 54 and 0.06738 respectively
I am wondering what the difference is between these two scenarios. They are both willcoxon signed-rank tests (not wilcoxon ranked sum), with the same assumptions and the same datapoints, yet the results are so different.
 A: Two-sample test. Suppose you have two independent normal samples, each of size 11:
set.seed(13)
x1 = rnorm(11, 142, 21)
x2 = rnorm(11, 123, 23)

To test whether these two samples come from populations with the same center, you
might do a Wilcoxon rank sum test. With P-value $0.01$ you reject $H_0,$ correctly finding that the data were sampled for distributions with different centers.
wilcox.test(x1, x2)

        Wilcoxon rank sum test

data:  x1 and x2
W = 99, p-value = 0.01038
alternative hypothesis: true location shift is not equal to 0

Inappropriate paired test. Because the two samples are independent, it is inappropriate to use a
paired Wilcoxon test (using argument pair=T, or using eleven differences
d = x1 - x2. In particular, the sample correlation $r_{12}$ is nowhere near $0.$
cor(x1, x2)
[1] -0.512896

Of course, because sample sizes happen to be the same, there is nothing to stop you from asking R to run a paired test [called a 'signed rank' test],
but the results will be meaningless, because the assumption of paired data
is not met.
wilcox.test(x1, x2, pair=T)

        Wilcoxon signed rank test

data:  x1 and x2
V = 54, p-value = 0.06738
alternative hypothesis: 
  true location shift is not equal to 0

Using differences, we get the same meaningless result; only the P-value is shown.
wilcox.test(x1-x2)$p.val
[1] 0.06738281

Appropriate paired test. If we have data sampled as paired, then a paired test is appropriate.
set.seed(14)
y1 = rnorm(11, 142, 20)
y2 = y1 + rnorm(11, -12, 3)
cor(y1,y2)
[1] 0.9914715

Results of the paired test to see if the differences $d_j = y_{1j}- y_{2j},\, j = 1,2, \dots, 11$ come from a population with mean (or median) $0.$ The
null hypothesis is correctly rejected because data were simulated with a difference in means.
Here is a stripchart of the eleven differences:

 wilcox.test(y1-y2)

        Wilcoxon signed rank test

data:  y1 - y2
V = 66, p-value = 0.0009766
alternative hypothesis: 
  true location is not equal to 0

wilcox.test(y1, y2, pair=T)$p.val
[1] 0.0009765625  # same P-alue as above

Notes: (1) Following your lead, I sampled data from normal populations. Thus,
it would have been appropriate to use a two-sample t test (preferably the
Welch version because of the different variances) in the first part and a paired t test in the second part.
(2) About syntax with wilcox.test: If there is one data argument, or if
there are two data arguments of the same size and parameter pair=T is present, then  R does a Wilcoxon signed rank test. If two data arguments
are present, then R does a Wilcoxon rank sum test (equivalent to a Mann-Whitney test). The two data vectors can be shown individually, or using
the 'stacked' format in which all data are in vector x, a second vector
g has 'subscripts' designating which group each element of x belongs to,
and a 'formula' with ~ showing the relationship:
set.seed(13)
x1 = rnorm(11, 142, 21)
x2 = rnorm(11, 123, 23)

x = c(x1, x2);  g = rep(1:2, each=11)
wilcox.test(x ~ g)$p.val
[1] 0.01038229    # same p-value as earlier

The 'formula' is also convenient for making side-by-side boxplots of the two
independent samples.
boxplot(x ~ g, col="skyblue2")


