I have done a PCA of a tibble df and I have obtained the list res.pca as a result.
I want to multiply the column CP2 in res.pca$rotation and in res.pca$x by -1.
I don't know how to multiply a constant by an element of a list.
library(tidyverse)
set.seed <- 1234
df <- tibble(a = runif(10),
b = runif(10),
c = runif(10),
d = runif(10),
e = runif(10),
f = runif(10))
res.pca <- prcomp(df, scale = TRUE)
# How to multiply column PC2 by -1 ?
res.pca$rotation
#> PC1 PC2 PC3 PC4 PC5 PC6
#> a -0.53522848 0.01623546 -0.17683875 0.53424822 -0.57591012 0.2547574
#> b 0.09345152 -0.10070446 -0.97591986 -0.09645054 0.06631633 -0.1224961
#> c -0.60060502 0.19351430 0.03334125 -0.04015348 0.18352003 -0.7519457
#> d -0.35361738 0.60631081 -0.11196246 -0.27535181 0.36203644 0.5365793
#> e 0.34230164 0.42828520 -0.04659627 0.74868543 0.34975861 -0.1198716
#> f 0.31914768 0.63332948 -0.02213681 -0.25945200 -0.61387997 -0.2288767
res.pca$x
#> PC1 PC2 PC3 PC4 PC5 PC6
#> [1,] -0.6358524 0.5828874 -1.38893192 0.8278318 0.45765671 0.36713422
#> [2,] 0.2867744 0.9462777 -0.02085629 -0.1507573 0.07456675 -0.85216056
#> [3,] 2.5912151 -1.3097803 -0.68040264 0.1440942 0.30874470 0.04728797
#> [4,] -0.9248741 -2.0135367 -0.46770160 -0.5386261 -0.08233173 -0.24589384
#> [5,] -1.7083258 0.1834963 0.32759227 1.0044239 0.49562619 -0.11892058
#> [6,] 0.4594589 2.1631258 0.32585525 0.4079981 -0.46661003 0.08308276
#> [7,] -1.5698588 0.6305786 -1.05203941 -1.6090313 -0.26425202 0.21223261
#> [8,] -1.2245220 -0.6453317 2.13533845 -0.3398981 0.22386580 0.23298046
#> [9,] 0.2014890 -1.0794984 0.10611970 1.0145475 -0.94717249 0.08065278
#> [10,] 2.5244957 0.5417812 0.71502618 -0.7605828 0.19990613 0.19360418
Created on 2021-07-13 by the reprex package (v2.0.0)