【问题标题】:Replacing rows in R替换 R 中的行
【发布时间】:2012-08-22 07:54:44
【问题描述】:

在 R 中,使用 cmets 作为 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

我正在通过一些单独的脚本修改此数据,该脚本维护行数/列数,并希望将其放回原始读取数据(使用用户 cmets)并像这样将其返回给用户

#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

有什么想法吗?

【问题讨论】:

  • 它究竟是如何改变数据的?如果它将因子转化为字符,或数据类型的类似变化,则可以解释 NA。
  • 此外,如果您强制列为数字,您将在任何列的注释行之后获得 NA。 R 并不是真的要与数据框一起读入评论数据,尽管您可以找到绕过它的方法。在任何情况下,您都必须更具体地了解您读取的数据类型以及您如何修改它
  • 我正在读取的数据是 5 列格式的数据:第 1-3 列(数字)第 4-5 列字符串在大多数情况下,我正在替换数据框特定单元格中的值(示例数据[5,8]=NA),有时替换整个列(示例数据[[3]]=1:100)我强迫R读取评论数据,因为当我将comment.char设置为'#'时,我丢失了评论行。因此,通过让 R 以这种方式阅读它,我可以提取未注释的行,将注释的行留在后面。至少这是我选择背后的逻辑
  • 为什么不编辑您的原始问题以包含一个完全可重现的示例?

标签: r comments rscript


【解决方案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))),]

【讨论】:

  • 啊!按行名排序,我没想到!奇迹般有效!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-25
  • 1970-01-01
  • 2011-09-26
  • 1970-01-01
  • 2011-05-12
  • 1970-01-01
相关资源
最近更新 更多