Reputation: 11995
I'm trying to create a .csv or .txt file from a data.frame
object that also contains some lines describing more detail about the variables.
Here's my first attempt:
Head <- "
#variables:
#sal - Salinity [PSU]
#temp - Temperature [degrees Celsius]
"
n <- 10
df <- data.frame(sal=runif(n, 30, 37), temp=runif(n, 15, 17))
df
sink("data.txt")
Head
df
sink()
which results in this:
[1] "\n#variables [units]:\n#sal - Salinity [PSU]\n#temp - Temperature [degrees Celcius]\n"
sal temp
1 32.11494 15.35176
2 30.57537 16.80972
3 32.90651 15.95174
4 30.62192 15.73436
5 31.43069 15.45873
6 34.38173 15.69713
7 31.27954 15.01126
8 32.77093 16.22493
9 35.99510 15.10123
10 35.52409 15.49084
but, I would really like it to look like this:
#variables [units]:
#sal - Salinity [PSU]
#temp - Temperature [degrees Celcius]
sal temp
1 32.11494 15.35176
2 30.57537 16.80972
3 32.90651 15.95174
4 30.62192 15.73436
5 31.43069 15.45873
6 34.38173 15.69713
7 31.27954 15.01126
8 32.77093 16.22493
9 35.99510 15.10123
10 35.52409 15.49084
Upvotes: 2
Views: 1245
Reputation: 44638
Use cat
instead of letting R call the object's print
method.
sink("data.txt")
cat(Head)
df
sink()
Upvotes: 4