R providing row numbers instead row names with read.csv?
0
0
Entering edit mode
3.9 years ago

Hi to all

i am reading my csv files into R with read.csv function and i did it and tried to see row names of the imported file and i am getting row names like numbers like this

"1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13",

but my row names are like this RED, RED1, RED2,,,,,, TILL RED12. I used this code to read

file1 <- read.csv("example.csv", header = TRUE, sep =",", stringsAsFactors=T).

can any one help to solve this? i used to read csv files with above code i never faced problem like this suprisingly this time only. any help in this regard would be highly appreciated.

Thanks in advance

R • 8.0k views
ADD COMMENT
0
Entering edit mode

I am a bit confused on what the dataframe actually looks like, can you do head(df) and paste the result here (df is name of your dataframe)? If you mean that there is an additional column which is displaying those rownames (1,2,3..) then have you tried getting rid of it by saying row.names(df) = NULL

ADD REPLY
0
Entering edit mode

hi manaswwm this is how my data in file INDIVIDUAL MALE FEMALE RED A/A A/A RED1 T/T T/T RED2 C/C C/C RED3 G/G G/G RED4 A/A A/A RED5 T/T T/T and if i tried head(df) i am getting like this INDIVIDUAL MALE FEMALE 1 RED A/A A/A 2 RED1 T/T T/T 3 RED2 C/C C/C 4 RED3 G/G G/G 5 RED4 A/A A/A 6 RED5 T/T T/T in head(df) i am getting exactly what i want, but when i tried row.names(df) i am getting only numbers in place of row names.

ADD REPLY
0
Entering edit mode

What do you get when you add the parameter row.names=1 ?

ADD REPLY
0
Entering edit mode

dear sam Thanks for your reply, its is reading row names when i use row.names=1 i am getting like this MALE FEMALE RED A/A A/A RED1 T/T T/T RED2 C/C C/C RED3 G/G G/G RED4 A/A A/A RED5 T/T T/T but when i tried head(df) it is not showing my first column name and i am unable to that column based on its name

ADD REPLY
0
Entering edit mode

Can you please try to format your posts a bit better -- it is almost impossible to discern the structure of your file from your previous posts. You can, for example, use the button with 101's to get appropriate code chunk formatting.

That being said, if head(df) gives you what you want after using file1 <- read.csv("example.csv", header = TRUE, sep =",", stringsAsFactors=T) you may simply need to add the row.names manually after reading the file in, e.g.:

file1 <- read.csv("example.csv", header = TRUE, sep =",", stringsAsFactors=T)
row.names(file1) <- file1$INDIVIDUAL
ADD REPLY
0
Entering edit mode

Hi friederike Good Morning Sorry for my not fine post here, i do not know how to post perfectly and thanks for your suggestion. Actually i want to one extra column (POP) in file1 based on INDIVIDUAL COLUMN from file2. please check this link for my example files and code code - https://www.dropbox.com/s/3l8m6ejkaytrer0/groupwise%20dendro%20grams%20code%20-%20Copy.txt?dl=0 file1 - https://www.dropbox.com/s/z5fxwm8mzxpdkrx/example1.csv?dl=0 file2 - https://www.dropbox.com/s/r0g1d9k25e8yppz/example2.csv?dl=0 the same worked for me in my previous work and all of a sudden it is throwing row numbers with out column heading and difficult to merge. I do not know what is reason for this sudden happening in R. Thanking you very much for your help

ADD REPLY
0
Entering edit mode

I tried running your code. Reading the csv and merging works. If you want the result to have row names, then you should use.

rownames(common_file) <- common_file$INDIVIDUAL
common_file <- common_file[,c("POP","MALE","FEMALE")]

The second line leaves all column names that are NOT the individual column name (not in the most efficient way... )

ADD REPLY
0
Entering edit mode

check ?read.csv to see the info about the row.names option:

a vector of row names. This can be a vector giving the actual row names, or a single number giving the column of the table which contains the row names, or character string giving the name of the table column containing the row names.

If there is a header and the first row contains one fewer field than the number of columns, the first column in the input is used for the row names. Otherwise if row.names is missing, the rows are numbered.

Using row.names = NULL forces row numbering.

ADD REPLY

Login before adding your answer.

Traffic: 2559 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6