我的数据框rep
如下所示:
> head(rep)
position chrom value label
[1,] "17408" "chr1" "0" "miRNA"
[2,] "17409" "chr1" "0" "miRNA"
[3,] "17410" "chr1" "0" "miRNA"
[4,] "17411" "chr1" "0" "miRNA"
[5,] "17412" "chr1" "0" "miRNA"
[6,] "17413" "chr1" "0" "miRNA"
如何从所有元素中删除引号?
注意:rep$position
和rep$value
应为numeric
类型,rep$chrom
和rep$label
应为character
类型。
答案 0 :(得分:9)
两个步骤:1)去除引号,2)相应地转换列:
数据
x <- read.table(text='
position chrom value label
"\\"17408\\"" "\\"chr1\\"" "\\"0\\"" "\\"miRNA\\""
"\\"17409\\"" "\\"chr1\\"" "\\"0\\"" "\\"miRNA\\""'
, header=T)
1)摆脱引号
library(stringr)
library(plyr)
del <- colwise(function(x) str_replace_all(x, '\"', ""))
x <- del(x)
2)相应地转换列
num <- colwise(as.numeric)
x[c(1,3)] <- num(x[c(1,3)])
x
position chrom value label
1 17408 chr1 0 miRNA
2 17409 chr1 0 miRNA
答案 1 :(得分:6)
如@Roland所示,您有一个matrix
,而不是data.frame
,这些方法有不同的默认print
方法。坚持使用matrix
,您可以在quote = FALSE
中明确设置print
,也可以使用noquote
。
这是一个基本的例子:
## Sample data
x <- matrix(c(17, "chr1", 0, "miRNA", 18, "chr1", 0, "miRNA"), nrow = 2,
byrow = TRUE, dimnames = list(
NULL, c("position", "chrom", "value", "label")))
## Default printing
x
# position chrom value label
# [1,] "17" "chr1" "0" "miRNA"
# [2,] "18" "chr1" "0" "miRNA"
## Two options to make the quotes disappear
print(x, quote = FALSE)
# position chrom value label
# [1,] 17 chr1 0 miRNA
# [2,] 18 chr1 0 miRNA
noquote(x)
# position chrom value label
# [1,] 17 chr1 0 miRNA
# [2,] 18 chr1 0 miRNA
另外,正如您自己想出的那样,将matrix
转换为data.frame
会使引号消失。如果每列是不同类型的数据(数字,字符,因子等),data.frame
是一种更适合保存数据的结构。但是,将matrix
转换为data.frame
并不会自动为您转换列。相反,您可以使用type.convert
(在使用data.frame
和系列创建read.table
时也会使用):
y <- data.frame(x, stringsAsFactors = FALSE)
str(y)
# 'data.frame': 2 obs. of 4 variables:
# $ position: chr "17" "18"
# $ chrom : chr "chr1" "chr1"
# $ value : chr "0" "0"
# $ label : chr "miRNA" "miRNA"
y[] <- lapply(y, type.convert)
str(y)
# 'data.frame': 2 obs. of 4 variables:
# $ position: int 17 18
# $ chrom : Factor w/ 1 level "chr1": 1 1
# $ value : int 0 0
# $ label : Factor w/ 1 level "miRNA": 1 1
y
# position chrom value label
# 1 17 chr1 0 miRNA
# 2 18 chr1 0 miRNA
答案 2 :(得分:2)
我想我找到了答案。我所拥有的不是data.frame而是矩阵。将它转换为data.frame摆脱了引号。我仍然想知道为什么......
rep <- data.frame(rep)
> head(rep)
position chrom value label
1 17408 chr1 0 miRNA
2 17409 chr1 0 miRNA
3 17410 chr1 0 miRNA
4 17411 chr1 0 miRNA
5 17412 chr1 0 miRNA
6 17413 chr1 0 miRNA