I have 2 dataframes, here are the codes:
set.seed(100)
x1= rnorm(4)
x2= rnorm(4)
x3= rnorm(4)
x4= rnorm(4)
x5= rnorm(4)
x6= rnorm(4)
x7= rnorm(4)
x8= rnorm(4)
x9= rnorm(4)
x10= rnorm(4)
df1 = data.frame(Station1 = x1, Station2 = x2, Station3 = x3, Station4 = x4, Station5 = x5, Station6 = x6, Station7 = x7, Station8 = x8, Station9 = x9, Station10 = x10)
x1= c("Station1", "Station2", "Station3", "Station4", "Station5", "Station6", "Station7", "Station8", "Station9", "Station10")
x2= seq(-2,10 , length=10)
x3= seq(30, 45, length=10)
x4= c(1, 3, 2, 1, 4, 2, 4, 3, 3, 1)
x5= seq(4, 16, length=10)
df2 = data.frame(Station=x1, Lon=x2, Lat=x3, Number=x4, Mis=x5)
Now I want to extract certain values of df1 and add them in a new column in df2. The df2$Number column goes from 1-4, just like the number of rows in df1. When there is a 1 in the df2$Number of Station1 I want to extract the value of the 1st row of df1 of the matching Station1. Another example would be: df2$Number for Station2 is 3, so I want to extract the value in the 3rd row of Station2 in df1, that would be -0.5817907.
All of these extracted values should be added in a new column in df2.
Here my examples:
>df1
Station1 Station2 Station3 Station4 Station5 Station6 Station7 Station8 Station9 Station10
1 -0.50219235 0.1169713 -0.82525943 -0.20163395 -0.3888542 -0.4380900 -0.8143791 -1.15772946 -0.1379296
2 0.13153117 0.3186301 -0.35986213 0.73984050 0.5108563 0.7640606 -0.4384506 0.24707599 -0.1111935
3 -0.07891709 -0.5817907 0.08988614 0.12337950 -0.9138142 0.2619613 -0.7202216 -0.09111356 -0.6900143
4 0.88678481 0.7145327 0.09627446 -0.02931671 2.3102968 0.7734046 0.2309445 1.75737562 -0.2217942 0.1829077 0.4173233 1.0654023 0.9702020
> df2
Station Lon Lat Number Mis
1 Station1 -2.0000000 30.00000 1 4.000000
2 Station2 -0.6666667 31.66667 3 5.333333
3 Station3 0.6666667 33.33333 2 6.666667
4 Station4 2.0000000 35.00000 1 8.000000
5 Station5 3.3333333 36.66667 4 9.333333
6 Station6 4.6666667 38.33333 2 10.666667
7 Station7 6.0000000 40.00000 4 12.000000
8 Station8 7.3333333 41.66667 3 13.333333
9 Station9 8.6666667 43.33333 3 14.666667
10 Station10 10.0000000 45.00000 1 16.000000
Here are Links to my actual dataframes: