我需要转换此表:
1 a b 0.689723476 0.149916917
a b 0.200907662 0.109557062
a b 0.684007597 0.703492299
a b 0.437375902 0.074223984
a b 0.090612241 0.146617232
a b 0.526821187 0.895595247
2 a b 0.507794544 0.731096076
a b 0.688987918 0.405801748
a b 0.462341505 0.203533346
a b 0.750096552 0.889744763
a b 0.555074241 0.06610472
进入这个:
1 a b 0.689723476 0.149916917
1 a b 0.200907662 0.109557062
1 a b 0.684007597 0.703492299
1 a b 0.437375902 0.074223984
1 a b 0.090612241 0.146617232
1 a b 0.526821187 0.895595247
2 a b 0.507794544 0.731096076
2 a b 0.688987918 0.405801748
2 a b 0.462341505 0.203533346
2 a b 0.750096552 0.889744763
2 a b 0.555074241 0.06610472
基本上,我需要向下重复第一行,直到出现一个新数字,然后重复该数字。我通常使用一种名为Multi-Row Formula的工具在Alteryx中执行此操作,其中我执行快速if语句,如下所示:
if IsNull([Row0:Column1]) then [Row-1:NewColumn] else [Row0:Column1 endif
这将创建一个新列,如果它为null,则取上述单元格中的值,否则它将复制新值。
R或此问题的任何其他解决方案中是否有类似内容?
更新
包含dput
数据:
structure(list(X1 = c(1L, NA, NA, NA, NA, NA, 2L, NA, NA, NA,
NA, NA, NA, NA, NA), X2 = c("a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a"), X3 = c("b", "b", "b",
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b"),
X4 = c(0.057520727, 0.54421869, 0.695381681, 0.866708518,
0.764246757, 0.622363342, 0.023082188, 0.140441859, 0.404509135,
0.806008319, 0.649705949, 0.607341742, 0.275179259, 0.199698605,
0.420250037), X5 = c(0.826617034, 0.338437818, 0.069566611,
0.95791903, 0.900005669, 0.533003641, 0.075841125, 0.200099759,
0.858293828, 0.271342591, 0.308531235, 0.344739272, 0.259006154,
0.824994839, 0.610793113)), .Names = c("X1", "X2", "X3",
"X4", "X5"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-15L), spec = structure(list(cols = structure(list(X1 = structure(list(), class = c("collector_integer",
"collector")), X2 = structure(list(), class = c("collector_character",
"collector")), X3 = structure(list(), class = c("collector_character",
"collector")), X4 = structure(list(), class = c("collector_double",
"collector")), X5 = structure(list(), class = c("collector_double",
"collector"))), .Names = c("X1", "X2", "X3", "X4", "X5")), default = structure(list(), class = c("collector_guess",
"collector"))), .Names = c("cols", "default"), class = "col_spec"))
答案 0 :(得分:0)
我们可以使用fill
包中的tidyr
函数。 dt
是您原始的数据框架。 dt2
是最终输出。
library(tidyr)
dt2 <- fill(dt, X1)