0

I know this question may be repeated but i tried all the solutions in :

How to convert entire dataframe to numeric while preserving decimals?

https://statisticsglobe.com/convert-data-frame-column-to-numeric-in-r

But didn't work

i imported excel data : from my computer manually :

File > import data > excel and i set the type of data as numeric

i checked my data using

View(Old_data) 

and it s true of type numeric

head(Old_data)
             QC_G.F9_01_4768   QC_G.F9_01_4765
M95T834        70027.02          69578.19   
M97T834        95774.14          81479.30
M105T541       75686.39          68455.65
M109T834       72093.07          70942.65
M111T834_2     77502.98          77527.54
M114T834       68132.06          70296.73
M121T834       52233.05          56074.64
M125T834       44559.99          35831.79
M128T834       59257.48          59574.73
M135T834       105136.55         105274.98

but after data i Converted rows into columns and columns into rows using R :

New_data <- as.data.frame(t(Old_data))

When i checked my new data using :

View(New_data)

I found that my columns are of type character and not numeric

i tried to convert New_data to numeric

New_data_B -> as.numeric(New_data)

i checked my data using

dim(New_data_B)

17 1091

Here's example of my data

New_data_B

#>                     Name    MT95T843     MT95T756
#> 1        QC_G.F9_01_4768 70027.02132  95774.13597
#> 2        QC_G.F9_01_4765 69578.18634  81479.29575
#> 3        QC_G.F9_01_4762 69578.18634  87021.95427
#> 4        QC_G.F9_01_4759 68231.14338  95558.76738
#> 5        QC_G.F9_01_4756 64874.12936  96780.77245
#> 6        QC_G.F9_01_4753 63866.65780  91854.35304
#> 7  CtrF01R5_G.D1_01_4757 66954.38799 128861.36163
#> 8  CtrF01R4_G.D5_01_4763 97352.55229 101353.25927
#> 9  CtrF01R3_G.C8_01_4754 61311.78576   7603.60896
#> 10 CtrF01R2_G.D3_01_4760 85768.36117 109461.75445
#> 11 CtrF01R1_G.C9_01_4755 85302.81947 104253.84537
#> 12 BtiF01R5_G.D7_01_4766 61252.42545 115683.73755
#> 13 BtiF01R4_G.D6_01_4764 81873.96379 112164.14229
#> 14 BtiF01R3_G.D2_01_4758 84981.21914      0.00000
#> 15 BtiF01R2_G.D4_01_4761 36629.02462 124806.49101
#> 16 BtiF01R1_G.D8_01_4767     0.00000 109927.26425
#> 17                    rt    13.90181     13.90586

also i converted my data to csv file and i imported it :

Old_data <- as.data.frame(read.csv("data.csv" , sep="," , header=TRUE,stringsAsFactors=FALSE))

And also using :

#install.packages("readxl")
library("readxl")
Old_data <- read_excel("data.xlsx")

I tried the solution suggested by Mr sveer

New_data <- cbind(Name=Old_data[1,],as.data.frame(t(Old_data[-1,])))

it gives this result

head(New_data)

When i tried

 View(New_data)
     Name.QC_G.F9_01_4768   Name.QC_G.F9_01_4765
      70027.02                69578.19   
      95774.14                81479.30
      75686.39                68455.65
      72093.07                70942.65
      77502.98                77527.54
      68132.06                70296.73
      52233.05                56074.64
      4559.99                 35831.79
      59257.48                59574.73
      105136.55               105274.98

it delets the rownames !

Im just confused of this problem, i think the problem is because i converted rows into columns and columns into rows

Please tell me for any clarification and also if i can send the data to someone so he can try Thank you very much

Reda
  • 449
  • 1
  • 4
  • 17
  • when you use ````read.csv````, you dont have to use ````as.data.frame````. which version of R are you using? The elements of column ````Name```` are strings. you may only want to transform two columns next to ````Name```` like ````library(dplyr); New_data_B <- New_data_B %>% mutate(MT95T843 = as.numeric(MT95T843), MT95T756 = as.numeric(MT95T756))```` – sveer Oct 24 '21 at 11:21
  • Thank you for your comment , yes i tried this solution but it didn't work , i used mutate_all function because i have 1091 columns and not only two – Reda Oct 24 '21 at 11:47
  • ````New_data_B[is.na(New_data_B),] <- 0; New_data_B <- bind_cols(New_data_B %>% select(Name), New_data_B %>% select(-Name) %>% mutate_all(as.numeric)```` – sveer Oct 24 '21 at 12:01

1 Answers1

0

Reason why you get character type and not numeric:

Transponsing the data will lead to a matrix. A matrix can take only a single class ie. character when there are mixed class.

Solution:

I am still not sure about the structure of your data. It is always a good idea to add a reproducible example, if the data is large you could also use pastebin or just reproduce as described.

I assume that when you load the data via: File > import data > excel that the first column is called "Name".

To get your desired output (especially rownames) you could try:

setNames(as.data.frame(t(Old_data[,-1])),Old_data[[1]]) -> df

If you want to transform the rownames to a column:

tibble::rownames_to_column(df, "Name")

captcoma
  • 1,768
  • 13
  • 29
  • Thanknyou for your answer , it gives this error message : "arguments imply different numbers of lines: 1091, 17" – Reda Oct 24 '21 at 18:21
  • Hello Mr captcoma , i just edited my question and i added what you asked , thanks – Reda Oct 25 '21 at 08:46
  • @Anouar I edited the code. Please add a reproducible dataset if it is not working. – captcoma Oct 27 '21 at 12:38