0

Hi i am trying to remove a variable name and dsiplay just the values alone. when doing so, i am getting a space above the first value.

Even when i tried to save it as a csv, that too had a space in the place of the variable names. How to avoid the space? Can anyone help?

I just wanted to display from the first value without any spaces in the place of the variable names.

Sample code:

unname(data.frame(cars$speed))

write.csv(unname(data.frame(cars$speed)), 'C:/Users/pdrajama/Downloads/Personal/R/cars.csv')

Console output:

unname(data.frame(cars$speed)) 

1   4
2   4
3   7
4   7
5   8
6   9
7  10
8  10
hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
Maharajaparaman
  • 141
  • 3
  • 12
  • 1
    All `unname()` is doing is removing the `name` attribute of all objects (column names and vector elements). The column names are still there, they're just empty. Have you _ever_ looked at the manual/help page for `write.csv`? That's done with `?write.csv`. Reading local manual pages is likely faster than taking the time to post a question. Anyway, there's a parameter there you should use to not include the `col.names` which you should be able to figure out without needing someone to read documentation for/to you. Please take some time to go through some basic R tutorials. – hrbrmstr Nov 18 '18 at 14:58
  • 1
    And if you *really* need to print to the console without headers, something like `x = capture.output(unname(cars)); cat(x[-1], sep = '\n')` could do it – dww Nov 18 '18 at 15:08
  • 3
    Possible duplicate of [Write a data frame to csv file without column header in R](https://stackoverflow.com/questions/19227243/write-a-data-frame-to-csv-file-without-column-header-in-r) or https://stackoverflow.com/questions/6750546/export-csv-without-col-names – dww Nov 18 '18 at 15:10
  • Sir tried that too.. it will display the variable name as False. or NA. – Maharajaparaman Nov 18 '18 at 15:10

1 Answers1

-1

You can try this:

write.table(data.frame(cars$speed),"test.txt",col.names=F,quote=F,sep=",")

write.table has already col.names=F to remove the first line of the data. And you can use sep="," to mimic the write.csv output.

Haktan Suren
  • 651
  • 7
  • 17