计算列百分比差异

时间:2017-05-14 19:19:36

标签: r

我的df有超过2列(例如,4)。我想计算第2-4列与第1列相比的百分比差异。

time1          time2            time3          time4
  1              5                1             10
  2              2                2              4
  3              6                3             12

我的代码是:

timepoint <- colnames(df)[2:4]
for (x in timepoint){
df$x <- 100*(df$x/df$time1-1)
}

这个功能有什么问题?谢谢!

1 个答案:

答案 0 :(得分:0)

也许这是一个太长的答案,但我认为这是一个开始步骤

library(dplyr)
library(tibble)
library(tidyr)

df <- tribble(
  ~time1, ~time2, ~time3, ~time4,
  1, 5, 1, 10,
  2, 2, 2, 4,
  3, 6, 3, 12
)

df_tidy <- df %>% 
  mutate(id = 1:nrow(.)) %>% 
  gather(time, value, time1:time4) %>% 
  mutate(id_base = ifelse(time == "time1", TRUE, FALSE))

df_calc <- filter(df_tidy, id_base == FALSE)

df_base <- df_tidy %>% 
  filter(id_base== TRUE) %>% 
  select(id, value_base = value)

df_join <- df_calc %>% 
  left_join(
    df_base, 
    by = "id"
  )

df_join %>% 
  mutate(diff = (value / value_base) * 100)
# A tibble: 9 × 4
     id  time value  diff
  <int> <chr> <dbl> <dbl>
1     1 time2     5   500
2     2 time2     2   100
3     3 time2     6   200
4     1 time3     1   100
5     2 time3     2   100
6     3 time3     3   100
7     1 time4    10  1000
8     2 time4     4   200
9     3 time4    12   400