Reputation: 2331
I would like my output to be print as each field beside each-other instead of all the values for the first field followed by all the values for the second
This is my script
clothing = c('coat_1','coat_1','coat_1','coat_2','coat_2','coat_2','scarf','scarf','scarf')
freq = c(0.3, 0.9, 0.1, 0.5, 0.3, 0.1, 0.4, 0.2, 0.1)
temp = c(10, 0, 20, 20, 15, 5, 30, 20, 10)
#exn = c(1, 1, 1, 2, 2, 2, 1, 1, 1)
df <- data.frame(clothing, freq, temp)
print(c(toString(df$clothing[df$temp>=20]), toString(df$temp[df$temp>=20])))
I get
[1] "coat_1, coat_2, scarf, scarf" "20, 20, 30, 20"
As output but I would like it output as
coot_1 20
coat_2 20
scarf 30
scarf 20
Upvotes: 0
Views: 373
Reputation: 32548
#Prepare your text
mytext = paste(paste(df$clothing[df$temp>=20], df$temp[df$temp>=20]), collapse = "\n")
#Display in console
cat(mytext)
#coat_1 20
#coat_2 20
#scarf 30
#scarf 20
#Write to a file
cat(mytext, file = "~/test.txt")
cat
can also append to an existing file. You may need to add a line break at the end of the previous text first
#Note this time I added a line break ("\n") at the end
mytext = paste0(paste(paste(df$clothing[df$temp>=20], df$temp[df$temp>=20]),
collapse = "\n"),"\n")
cat(mytext, file = "~/test.txt") #Check the output file
cat(mytext, file = "~/test.txt", append = TRUE) #Check the output file again
Upvotes: 1
Reputation: 1764
Edited to include step to output to file...
df.print <- df[which(df$temp>=20),c('clothing', 'temp')]
print(df.print)
write.csv(df.print, file = "c:\\myname\\yourfile.csv")
Upvotes: 0