我有很多.txt文件,通常有5列,但有些行有更多,例如:
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e,f,g
a,b,c,d,e
Run Code Online (Sandbox Code Playgroud)
我想要做的就是粘贴比第五列更远的所有列.上面的例子应该导致:
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e f g
a,b,c,d,e
Run Code Online (Sandbox Code Playgroud)
我怎么能用R编程?
我假设您已经将".csv"文件读入R,通过:
dat <- read.csv(file, header = FALSE, fill = TRUE)
Run Code Online (Sandbox Code Playgroud)
对您提供的数据进行一点测试:
x <- "a,b,c,d,e
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e,f,g
a,b,c,d,e"
dat <- read.csv(text = x, header = FALSE, fill = TRUE)
# V1 V2 V3 V4 V5 V6 V7
#1 a b c d e
#2 a b c d e
#3 a b c d e
#4 a b c d e f g
#5 a b c d e
Run Code Online (Sandbox Code Playgroud)
这可能是另一种可能吗
from <- 5
dat[, from] <- do.call(paste, dat[from:ncol(dat)]) ## merge and overwrite
dat[, (from+1):ncol(dat)] <- NULL ## drop
# V1 V2 V3 V4 V5
#1 a b c d e
#2 a b c d e
#3 a b c d e
#4 a b c d e f g
#5 a b c d e
Run Code Online (Sandbox Code Playgroud)
我的简单方法需要你from事先知道; 但似乎你知道它.