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

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

How to remove scientific notation from a dataframe in R

问题

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

  1. options(scipen=999)
  2. chr9_mod <- chr9[26:3531,26:3531]
  3. bed_file_position_hic <- as.data.frame(matrix(ncol=3,nrow=3506))
  4. colnames(bed_file_position_hic) <- c("chrom","start","end")
  5. j <- 1
  6. k <- 3506
  7. for(i in 1){
  8. bed_file_position_hic$start[j:k] <- rownames(chr9_mod)[i]
  9. bed_file_position_hic$end[j:k] <- colnames(chr9_mod)[1:3506]
  10. j <- k +1
  11. k <- k+1+3505
  12. }
  13. bed_file_position_hic$start <- format(bed_file_position_hic$start,scientific=F)

但它没有修改结果。

我的数据框如下所示:

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

chr9_mod的输入数据如下:

  1. dput(chr9_mod[1:4,1:4])
  2. structure(list(`1e+06` = c(0, 0, 160.414829465374, 110.329932711188
  3. ), `1040000` = c(0, 0, 0, 176.764122366621), `1080000` = c(160.414829465374,
  4. 0, 0, 0), `1120000` = c(110.329932711188, 176.764122366621, 0,
  5. 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.

  1. options(scipen=999)
  2. chr9_mod <- chr9[26:3531,26:3531]
  3. bed_file_position_hic <- as.data.frame(matrix(ncol=3,nrow=3506))
  4. colnames(bed_file_position_hic) <- c("chrom","start","end")
  5. j <- 1
  6. k <- 3506
  7. for(i in 1){
  8. bed_file_position_hic$start[j:k] <- rownames(chr9_mod)[i]
  9. bed_file_position_hic$end[j:k] <- colnames(chr9_mod)[1:3506]
  10. j <- k +1
  11. k <- k+1+3505
  12. }
  13. bed_file_position_hic$start <- format(bed_file_position_hic$start,scientific=F)

But it doesn't modify the results.

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

My input data for chr9_mod is:

  1. dput(chr9_mod[1:4,1:4])
  2. structure(list(`1e+06` = c(0, 0, 160.414829465374, 110.329932711188
  3. ), `1040000` = c(0, 0, 0, 176.764122366621), `1080000` = c(160.414829465374,
  4. 0, 0, 0), `1120000` = c(110.329932711188, 176.764122366621, 0,
  5. 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 类型后,对它们进行格式化,你应该会得到你所寻求的结果。

  1. > bed_file_position_hic$start <-
  2. + format(as.numeric(bed_file_position_hic$start), scientific = FALSE)
  3. > bed_file_position_hic$end<-
  4. + format(as.numeric(bed_file_position_hic$end), scientific = FALSE)
  5. >;
  6. > bed_file_position_hic[, c("start", "end")]
  7. start end
  8. 1 1000000 1000000
  9. 2 1000000 1040000
  10. 3 1000000 1080000
  11. 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.

  1. &gt; bed_file_position_hic$start &lt;-
  2. + format(as.numeric(bed_file_position_hic$start), scientific = FALSE)
  3. &gt; bed_file_position_hic$end&lt;-
  4. + format(as.numeric(bed_file_position_hic$end), scientific = FALSE)
  5. &gt;
  6. &gt; bed_file_position_hic[, c(&quot;start&quot;, &quot;end&quot;)]
  7. start end
  8. 1 1000000 1000000
  9. 2 1000000 1040000
  10. 3 1000000 1080000
  11. 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:

确定