2

I am learning R and have a little knowledge in Python. I feel that R and Python are in some parts alike, and in others very much unalike.

Especially when iterating or reading data from some file, I get stuck.

I have a data.frame

temp <- 
A    B    C
1    2    3
4    5    6
7    8    8

I simply want to read it out, row by row, and write it to a new file.

#New File
[1,2,"3"],
[4,5,"6"],
[7,8,"9"],

I tried this

A <- temp[1]
B <- temp[2]
C <- temp[3]

print(paste("[", A, B, as.character(C),  "]", sep = ","))

But, that paste does not combine A, B and C correct. It gives me this:

[1,4,"7"],
[2,5,"8"],
[3,6,"9"],

I tried several other things like

`test <- rbind(temp)` 

or

for(i in temp) {
        print(temp[,])
        print(paste("[", temp[,],  "]", sep = ","))
}

and in the end it always comes down to the same thing: It arrays it column by column when I try to print/paste it instead of row by row.

I also fiddled around with array(). It feels like the right function to do this, but passing list as the FUN argument does not seem right...

four-eyes
  • 10,740
  • 29
  • 111
  • 220

2 Answers2

3

Here is one solution, writing your data into myfile.txt:

cat(apply(temp, 1, function(x)paste0('[', x[1], ' ',  x[2], ' "', x[3], '"]', '\n')), sep='', file='myile.txt')
user1981275
  • 13,002
  • 8
  • 72
  • 101
  • Thanks for your help. What is `function(x)` though? – four-eyes Jul 23 '15 at 09:49
  • Have a look at `?apply`. `function(x)` is a function which takes a row of your dataframe as input vector, prints the values in brackets with the third element quoted. – user1981275 Jul 23 '15 at 09:52
  • yes, I understood that. But `function(x)` ist jsut a dummy?! Why not using the `paste()` function instead of `function(x)`? – four-eyes Jul 23 '15 at 09:59
  • Because something like `apply(temp, 1, paste0)` would not let you distinguish between the different elements in the vector which `paste0` takes. This becomes possible when wrapping paste0 in a custom function. – user1981275 Jul 23 '15 at 10:09
  • Ah, now I get it. Thanks! – four-eyes Jul 23 '15 at 10:50
1

(Native) python and R work very differently when it comes to arrays/vectors/matrices. Python must be implicit instructed to iterate over each element, while R does this natively.

If your desired output is [1,2,"3"],\n[4,5,"6"],\n[7,8,"9"],\n you can do it in two steps (although another answer has a more direct solution, I included this for learning purposes):

#1 concatenate each row:
rows <- apply(temp, 1, function(x) paste0('[', x[1], ' ',  x[2], ' "', x[3], '"]'))
#2 write rows to file
writeLines(rows, con="myile.txt')
MrGumble
  • 5,631
  • 1
  • 18
  • 33
  • Yes, that works very differently and I just started to learn R. However, I messed around with the `apply()` function, but I did not know which function to fill in there... you used (as the writer before) `function(x)`. What is this? – four-eyes Jul 23 '15 at 09:56
  • ``function(x)`` is a lambda function or anonymous function that only exists when executing that line. – MrGumble Jul 23 '15 at 12:46