0

I have my data in a csv file. When I use the read.csv function for create a variable, apparently R assumes the first column like labels but my data is organized differently. In the csv file is the first row with the labels and each column contains the corresponding numbers. When I run the read.csv function R introduce a new cell with the "row.names" label and displace my original labels. I have consulted numerous tutorials and websites, including AI, but I have not been able to solve it. Can anyone give me their opinion on this? Has the same thing happened to anyone?

I hope to get some advices on this

  • 6
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Aug 09 '23 at 15:23
  • put the command your used, like `my_data <- read.csv('file', header=TRUE, sep = ','), then dput(head(my_data, n = 6)), copy 'structure(...}` above as data. – Chris Aug 09 '23 at 15:30
  • 1
    R typically only assumes you have a row names column when you have one fewer column heading in the first row. It's really hard to know what's going on without seeing the data. – MrFlick Aug 09 '23 at 15:31
  • If you could post about 5 rows of your text file that would help a lot. (If you have a lot of columns, we only need the first few columns.) – Gregor Thomas Aug 09 '23 at 15:56
  • Thanks for the quick reply. Here I leave a section of my text file and my command line. – Pedro Valdés Cuadra Aug 09 '23 at 18:22
  • complex_1,complex_2,complex_3,complex_4,complex_5 1.7314931154251099,1.2897676229476929,1.4534262418746948,1.8176230192184448,1.758054494857788, 1.774179220199585,1.421286940574646,1.589133381843567,2.1844005584716797,1.8542134761810303, 1.730838656425476,1.3490982055664063,1.809985637664795,2.197779893875122,2.0701870918273926, 1.7465367317199707,1.493726134300232,1.8040440082550049,2.323239803314209,2.264295816421509, 1.7999173402786255,1.529505729675293,2.0102133750915527,2.433931350708008,2.283825635910034, – Pedro Valdés Cuadra Aug 09 '23 at 18:28
  • My command line was: datos <- read.csv("data.csv", header = TRUE, row.names = NULL, check.names = FALSE). Keep in mind that I tried to vary parameters but without good results – Pedro Valdés Cuadra Aug 09 '23 at 18:37
  • Please edit the sample from your CSV into your question so things like line breaks come through. – Gregor Thomas Aug 10 '23 at 17:14

1 Answers1

2

I believe the problem is the "," at the end of each data row. If you do not mind importing the data with tidyverse, you may try:

library(tidyverse)
df <- read_csv("data.csv") # using all default parameters should work, I found that tidyverse is pretty smart
William Wong
  • 453
  • 2
  • 9