根据列对行进行排序?

Sort rows based on columns?

我在 R 中有一个 csv,其中包含以下 table:

df1

Start Efficiency Start Efficiency End
9955 0 9955 25 5848
474909 0 474909 35 3948
90342 25 84384
94209 55 99393

我想用正确的效率替换对应于起始列的假零效率,以便我的最终结果如下所示:

df2

Start Efficiency End
9955 25 5848
474909 35 3948
90342 25 84384
94209 55 99393
df1 <- data.frame(
  Start = c("9955", "474909", "90342", "94209"),
  Efficiency = c("0", "0", "25", "55"), 
  Start = c("9955", "474909"),
  Efficiency = c("25", "35"), 
  End = c("5848", "3948", "84384", "99393"), stringsAsFactors = F)
df2 <- data.frame(
  Start = c("9955", "474909", "90342", "94209"),
  Efficiency = c("25", "35", "25", "55"),
  End = c("5848", "3948", "84384", "99393"), stringsAsFactors = F)

R 不喜欢具有相同名称的列,因此重复的列名称变为 Start.1Efficiency.1。您可以将 0 值替换为 NA 并使用 coalesce.

library(dplyr)

df1 %>%
  mutate(across(contains('Efficiency'), na_if, 0)) %>%
  transmute(Start, Efficiency = coalesce(Efficiency, Efficiency.1))

#   Start Efficiency
#1   9955         25
#2 474909         35
#3  90342         25
#4  94209         55