Search code examples
rdataframeformatscientific-notation

How to remove scientific notation from a dataframe in R


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")

Solution

  • 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.

    > 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