R - 根据多个条件提取值

时间:2018-01-27 22:16:46

标签: r dataframe extract

我有2个数据帧,下面是代码:

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)

现在我想提取df1的某些值并将它们添加到df2中的新列中。 df2 $ Number列从1-4开始,就像df1中的行数一样。当df2 $中的1为1时,我想提取匹配的Station1的第1行df1的值。另一个例子是:df2 $ Station2的数字是3,所以我想在df1中提取Station2第3行的值,即-0.5817907。

所有这些提取的值都应添加到df2中的新列中。

这是我的例子:

>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

以下是我实际数据框的链接:

df1:https://megastore.uni-augsburg.de/get/IftpNjXNqL/

df2:https://megastore.uni-augsburg.de/get/8_Y6SPVCA9/

3 个答案:

答案 0 :(得分:2)

df2$Station <- as.numeric(gsub("[^0-9]", "", df2$Station))
df2$New <- apply(df2, 1, function(x) df1[x[4], x[1]])

df2

   Station        Lon      Lat Number       Mis         New
1        1 -2.0000000 30.00000      1  4.000000 -0.50219235
2        2 -0.6666667 31.66667      3  5.333333 -0.58179068
3        3  0.6666667 33.33333      2  6.666667 -0.35986213
4        4  2.0000000 35.00000      1  8.000000 -0.20163395
5        5  3.3333333 36.66667      4  9.333333  2.31029682
6        6  4.6666667 38.33333      2 10.666667  0.76406062
7        7  6.0000000 40.00000      4 12.000000  0.23094453
8        8  7.3333333 41.66667      3 13.333333 -0.09111356
9        9  8.6666667 43.33333      3 14.666667 -0.69001432
10      10 10.0000000 45.00000      1 16.000000  0.18290768

这是一个选项 - 只需将Station列更改为数字以进行索引,并通过使用apply

循环来获取值

编辑:对于您的真实数据,列索引是不同的,正则表达式可能会或可能不起作用 - 所以我会这样做:

MainFrame$New <- apply(MainFrame, 1, function(x) Centroids[x[3], x[1]])

答案 1 :(得分:2)

传统的数据库方法会将df1转换为长/高数据集。然后使用左连接执行选择(使用dplyrdata.table或甚至base::merge()等包。

library(magrittr)
df_value_long <- df1 %>% 
  dplyr::mutate(
    Number   = seq_len(n())
  ) %>% 
  tidyr::gather(Station, value, -Number)

df2b <- df2 %>% 
  dplyr::left_join(df_value_long, by=c("Station", "Number"))

df_value_long

   Number   Station       value
1       1  Station1 -0.50219235
2       2  Station1  0.13153117
3       3  Station1 -0.07891709
4       4  Station1  0.88678481
5       1  Station2  0.11697127
6       2  Station2  0.31863009
7       3  Station2 -0.58179068
8       4  Station2  0.71453271
...

df2b

     Station        Lon      Lat Number       Mis       value
1   Station1 -2.0000000 30.00000      1  4.000000 -0.50219235
2   Station2 -0.6666667 31.66667      3  5.333333 -0.58179068
3   Station3  0.6666667 33.33333      2  6.666667 -0.35986213
...

答案 2 :(得分:1)

df1$NEW=df1[cbind(df2$Number,1:10)]
df1
     Station        Lon      Lat Number       Mis         NEW
1   Station1 -2.0000000 30.00000      1  4.000000 -0.50219235
2   Station2 -0.6666667 31.66667      3  5.333333 -0.58179068
3   Station3  0.6666667 33.33333      2  6.666667 -0.35986213
4   Station4  2.0000000 35.00000      1  8.000000 -0.20163395
5   Station5  3.3333333 36.66667      4  9.333333  2.31029682
6   Station6  4.6666667 38.33333      2 10.666667  0.76406062
7   Station7  6.0000000 40.00000      4 12.000000  0.23094453
8   Station8  7.3333333 41.66667      3 13.333333 -0.09111356
9   Station9  8.6666667 43.33333      3 14.666667 -0.69001432
10 Station10 10.0000000 45.00000      1 16.000000  0.18290768