簡體   English   中英

R根據另一組列設置值

[英]R setting a value based on another column by group

我在R中有一個數據框,看起來像下面的數據框。 我想創建一個新的名為列tfp level[1980]它利用了1980年價值tfp level 考慮到按國家分組。

因此,例如,澳大利亞每年將采用0.796980202,哥斯達黎加每年將采用1.082085967。

country     ISO year    tfp level    tfp level[1980]
Australia   AUS 1980    0.796980202 
Australia   AUS 1981    0.808527768 
Australia   AUS 1982    0.790943801 
Australia   AUS 1983    0.818122745 
Australia   AUS 1984    0.827925146     
Australia   AUS 1985    0.825170755 
Costa Rica  CRI 1980    1.082085967 
Costa Rica  CRI 1981    1.033975005 
Costa Rica  CRI 1982    0.934024811 
Costa Rica  CRI 1983    0.920588791

必須有一種方法可以使用dplyr巧妙地解決此問題,例如使用group_by命令,但是我自己卻找不到一個好的解決方案。

謝謝。

按“國家”分組后,進行mutate以獲取“年”值1980的相應“ tfp.level”

library(dplyr)
df1 %>% 
  group_by(country) %>%
  mutate(tfllevel1980 = `tfp level`[year == 1980])
# A tibble: 10 x 5
# Groups:   country [2]
#   country    ISO    year `tfp level` tfllevel1980
#   <chr>      <chr> <int>       <dbl>        <dbl>
# 1 Australia  AUS    1980       0.797        0.797
# 2 Australia  AUS    1981       0.809        0.797
# 3 Australia  AUS    1982       0.791        0.797
# 4 Australia  AUS    1983       0.818        0.797
# 5 Australia  AUS    1984       0.828        0.797
# 6 Australia  AUS    1985       0.825        0.797
# 7 Costa Rica CRI    1980       1.08         1.08 
# 8 Costa Rica CRI    1981       1.03         1.08 
# 9 Costa Rica CRI    1982       0.934        1.08 
#10 Costa Rica CRI    1983       0.921        1.08 

或使用base R

df1$tfplevel1980 <- with(df1, ave(`tfp level` * (year == 1980), 
                 country, FUN = function(x) x[x!= 0]))

數據

df1 <- structure(list(country = c("Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Costa Rica", "Costa Rica", 
"Costa Rica", "Costa Rica"), ISO = c("AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "CRI", "CRI", "CRI", "CRI"), year = c(1980L, 1981L, 
1982L, 1983L, 1984L, 1985L, 1980L, 1981L, 1982L, 1983L), 
`tfp level` = c(0.796980202, 
0.808527768, 0.790943801, 0.818122745, 0.827925146, 0.825170755, 
1.082085967, 1.033975005, 0.934024811, 0.920588791)),
class = "data.frame", row.names = c(NA, 
-10L))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM