有没有办法根据条件从 R 脚本中 select 行?
Is there a way to select rows from an R script by condition?
该问题与 select 数据框或通用 R 对象的行无关,而是如前所述,与 select 脚本的行有关。
例如。我怎样才能 select 来自 R 脚本的所有奇数行?
R
txt <- readLines("path/to/script.R")
writeLines(txt[seq_along(txt) %% 2 == 0], "path/to/script_evenlines.R")
writeLines(txt[seq_along(txt) %% 2 == 1], "path/to/script_oddlines.R")
sed
(在 bash 或类似的 shell 提示符下。)
sed -ne '0-2p' path/to/script.R > path/to/script_evenlines.R
sed -ne '1~2p' path/to/script.R > path/to/script_oddlines.R
这可以扩展到 每 N
行节省
sed -ne '0-Np' path/to/script.R
或删除每N
行
sed -e '0-Nd' path/to/script.R
该问题与 select 数据框或通用 R 对象的行无关,而是如前所述,与 select 脚本的行有关。 例如。我怎样才能 select 来自 R 脚本的所有奇数行?
R
txt <- readLines("path/to/script.R")
writeLines(txt[seq_along(txt) %% 2 == 0], "path/to/script_evenlines.R")
writeLines(txt[seq_along(txt) %% 2 == 1], "path/to/script_oddlines.R")
sed
(在 bash 或类似的 shell 提示符下。)
sed -ne '0-2p' path/to/script.R > path/to/script_evenlines.R
sed -ne '1~2p' path/to/script.R > path/to/script_oddlines.R
这可以扩展到 每 N
行节省
sed -ne '0-Np' path/to/script.R
或删除每N
行
sed -e '0-Nd' path/to/script.R