如何在R中从数据框中删除科学计数法。

huangapple go评论64阅读模式
英文:

How to remove scientific notation from a dataframe in R

问题

我想要从数据框中移除科学计数法。我的数据框如下所示:我想要将1e6修改为1000000的值。是否有任何方法可以做到这一点?我尝试了format选项和scipen选项。

options(scipen=999) 
chr9_mod <- chr9[26:3531,26:3531]
bed_file_position_hic <- as.data.frame(matrix(ncol=3,nrow=3506))
colnames(bed_file_position_hic) <- c("chrom","start","end")
j <- 1
k <- 3506
for(i in 1){
  bed_file_position_hic$start[j:k] <- rownames(chr9_mod)[i]
  bed_file_position_hic$end[j:k] <- colnames(chr9_mod)[1:3506]
  j <- k +1
  k <- k+1+3505
}

bed_file_position_hic$start <- format(bed_file_position_hic$start,scientific=F)

但它没有修改结果。

我的数据框如下所示:

dput(bed_file_position_hic[1:4,1:3])
structure(list(chrom = c(NA, NA, NA, NA), start = c("1e+06", 
"1e+06", "1e+06", "1e+06"), end = c("1e+06", "1040000", "1080000", 
"1120000")), row.names = c(NA, 4L), class = "data.frame")

chr9_mod的输入数据如下:

dput(chr9_mod[1:4,1:4])
structure(list(`1e+06` = c(0, 0, 160.414829465374, 110.329932711188
), `1040000` = c(0, 0, 0, 176.764122366621), `1080000` = c(160.414829465374, 
0, 0, 0), `1120000` = c(110.329932711188, 176.764122366621, 0, 
0)), row.names = c("1e+06", "1040000", "1080000", "1120000"), class = "data.frame")
英文:

I want to remove scientific notation from a dataframe.
My dataframe look like this:
I want to modify the 1e6 to 1000000 value.
Is there any way to do this
I tried the format option and scipen option as well.

options(scipen=999) 
chr9_mod <- chr9[26:3531,26:3531]
bed_file_position_hic <- as.data.frame(matrix(ncol=3,nrow=3506))
colnames(bed_file_position_hic) <- c("chrom","start","end")
j <- 1
k <- 3506
for(i in 1){
  bed_file_position_hic$start[j:k] <- rownames(chr9_mod)[i]
  bed_file_position_hic$end[j:k] <- colnames(chr9_mod)[1:3506]
  j <- k +1
  k <- k+1+3505
}

bed_file_position_hic$start <- format(bed_file_position_hic$start,scientific=F)

But it doesn't modify the results.

My dataframe look like: 
dput(bed_file_position_hic[1:4,1:3])
structure(list(chrom = c(NA, NA, NA, NA), start = c("1e+06", 
"1e+06", "1e+06", "1e+06"), end = c("1e+06", "1040000", "1080000", 
"1120000")), row.names = c(NA, 4L), class = "data.frame")

My input data for chr9_mod is:

dput(chr9_mod[1:4,1:4])
structure(list(`1e+06` = c(0, 0, 160.414829465374, 110.329932711188
), `1040000` = c(0, 0, 0, 176.764122366621), `1080000` = c(160.414829465374, 
0, 0, 0), `1120000` = c(110.329932711188, 176.764122366621, 0, 
0)), row.names = c("1e+06", "1040000", "1080000", "1120000"), class = "data.frame")

答案1

得分: 1

目前 bed_file_position_hic$startbed_file_position_hic$end 被存储为 character 类型。

在将它们转换为 numeric 类型后,对它们进行格式化,你应该会得到你所寻求的结果。

> bed_file_position_hic$start <- 
+    format(as.numeric(bed_file_position_hic$start), scientific = FALSE)
> bed_file_position_hic$end<- 
+    format(as.numeric(bed_file_position_hic$end), scientific = FALSE)
>;
> bed_file_position_hic[, c("start", "end")]
    start     end
1 1000000 1000000
2 1000000 1040000
3 1000000 1080000
4 1000000 1120000
英文:

Currently bed_file_position_hic$start and bed_file_position_hic$end are being stored as character types.

Format them after converting them into numeric types, and you should get the results you're looking for.

&gt; bed_file_position_hic$start &lt;- 
+    format(as.numeric(bed_file_position_hic$start), scientific = FALSE)
&gt; bed_file_position_hic$end&lt;- 
+    format(as.numeric(bed_file_position_hic$end), scientific = FALSE)
&gt;
&gt; bed_file_position_hic[, c(&quot;start&quot;, &quot;end&quot;)]
    start     end
1 1000000 1000000
2 1000000 1040000
3 1000000 1080000
4 1000000 1120000

huangapple
  • 本文由 发表于 2023年4月7日 01:16:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952133.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定