gpt4 book ai didi

从数据框中删除所有引号

转载 作者:行者123 更新时间:2023-12-03 18:26:07 25 4
gpt4 key购买 nike

我有一个数据框 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$positionrep$value应该是 numeric类型, rep$chromrep$label应该是 character类型。

最佳答案

两个步骤: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

关于从数据框中删除所有引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21658113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com