替换R中的行

时间:2012-08-27 19:45:11

标签: r comments rscript

在R am中使用

读取带有注释为csv的文件
read.data.raw = read.csv(inputfile, sep='\t', header=F, comment.char='')

该文件如下所示:

#comment line 1
data 1<tab>x<tab>y
#comment line 2
data 2<tab>x<tab>y
data 3<tab>x<tab>y

现在我使用

提取未注释的行
comment_ind = grep( '^#.*', read.data.raw[[1]])
read.data = read.data.raw[-comment_ind,]

离开了我:

 data 1<tab>x<tab>y
 data 2<tab>x<tab>y
 data 3<tab>x<tab>y

我正在通过一些单独的脚本修改这些数据,该脚本维护行/列的数量,并希望将其放回原始读取数据(带有用户注释)并将其返回给用户,如下所示

#comment line 1
modified data 1<tab>x<tab>y
#comment line 2
modified data 2<tab>x<tab>y
modified data 3<tab>x<tab>y

由于我在read.data中提取的数据保留了行名称row.names(read.data),我试过了

original.read.data[as.numeric(row.names(read.data)),] = read.data

但那不起作用,我得到了一堆NA / s

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

这样做你想要的吗?

read.data.raw <- structure(list(V1 = structure(c(1L, 3L, 2L, 4L, 5L),
   .Label = c("#comment line 1", "#comment line 2", "data 1", "data 2", 
   "data 3"), class = "factor"), V2 = structure(c(1L, 2L, 1L, 2L, 2L), 
   .Label = c("", "x"), class = "factor"), V3 = structure(c(1L, 2L, 1L,
   2L, 2L), .Label = c("", "y"), class = "factor")), .Names = c("V1", 
   "V2", "V3"), class = "data.frame", row.names = c(NA, -5L))

comment_ind = grep( '^#.*', read.data.raw[[1]])
read.data <- read.data.raw[-comment_ind,]
# modify V1
read.data$V1 <- gsub("data", "DATA", read.data$V1)
# rbind() and then order() comments into original places
new.data <- rbind(read.data.raw[comment_ind,], read.data)
new.data <- new.data[order(as.numeric(rownames(new.data))),]