0

I have several variables that I want to save to a .csv file. These variables have different values, which creates an error at the following command:

mojeDane <- data.frame('Numer'=waga_linkow, 'Zarażenie nr 1.'= selekcja, Zarażenie nr 2.'=selekcja2)
write.csv2(mojeDane, 'wynik.csv', row.names = F, na = '', fileEncoding = 'Windows-1250')
Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
Nerson48
  • 1
  • 6
  • 3
    Please read this on how to make a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). We don't know the error. We don't know how your mojeDane data.frame looks like. – phiver May 05 '18 at 14:31
  • 2
    Also proofread for run-on sentences, punctuation, and capitalization. Make sure your question is clear, understandable, and includes all necessary information. [How to Ask a Question on StackOverflow](https://stackoverflow.com/help/how-to-ask), [writing the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) – SherylHohman May 05 '18 at 19:30

1 Answers1

0

It looks like you have a typo in the definition of your data.frame. The name of the third column is missing the quote at the beginning.

Data.frames in R can only have the same number of elements for each column. For example, this works:

# define vectors of the same length
waga_linkow <- rnorm(10)
selekcja <- 1:10
selekcja2 <- c(rep("wieczór", 5), rep("dzień",5))

# combine vectors into a data frame
mojeDane = data.frame('Numer'= waga_linkow , 'Zarażenie nr 1.'= selekcja, 'Zarażenie nr 2'= selekcja2)

# save the data    
write.csv2(mojeDane, 'wynik.csv', row.names = F, na = '', fileEncoding = 'Windows-1250')

If you original values are vectors or various lengths, then you can save them as a list and then save it in Rdata format:

# define vectors of the various length
waga_linkow <- rnorm(10)
selekcja <- 1:20
selekcja2 <- c(rep("wieczór", 5), rep("dzień",1))

# save as a list
mojeDane.list = list('Numer'= waga_linkow , 'Zarażenie nr 1.'= selekcja, 'Zarażenie nr 2'= selekcja2)

# Save to a file
save(mojeDane.list, file="wynik.Rdata")

To load Rdata file back into R use load() function

If you nned to save columns of different lenght in csv.format you can add missing values to the shorter vectors and write the output with na=' ':

# Assuming selekcja is the longest vector in the data
df <- data.frame('Zarażenie nr 1.'= selekcja,
                 'Numer' = waga_linkow[seq(selekcja)], 
                 'Zarażenie nr 2' = selekcja2[seq(selekcja)])

# Assuming you want spaces for missing values
write.csv(df, "output.csv",row.names=F, quote=FALSE, na=" ")

enter image description here

Katia
  • 3,784
  • 1
  • 14
  • 27
  • can you save columns of different vector lengths in the .cvs file? this is my error: Error in data.frame(Numer = chory, `Zarażenie nr 1.` = selekcja, `Zarażenie nr 2` = selekcja2) : arguments imply differing number of rows: 5, 8, 16 – Nerson48 May 06 '18 at 09:25
  • I need a result in .csv format. They are needed for engineering work – Nerson48 May 06 '18 at 09:38
  • I added solution to the answer. See if this helps you. – Katia May 06 '18 at 10:47
  • You need them in one big line separating each row by spaces? You can add to write.csv file option eol = " " – Katia May 06 '18 at 11:11
  • `> length (waga_linkow) [1] 5` `>> length (selekcja) [1] 8``> length (selekcja2) [1] 18` Your solution only saves the first 5 (as the length of "weight_links"), the rest is skipped. Maybe it can be exported from .RData to xlsx (using the 'xlsx' library). What do you think ? – Nerson48 May 06 '18 at 11:27
  • Sorry, I cannot answer you in Polish :). The bottom part in my solution creates the dataframe with the number of rows based on the longest vector ( I assumed seleksja in my example). In this dataframe all vectors that are shorter are extended with "NA"(missing values) and then when I output this dataframe I use space ("na=" ") for NAs. – Katia May 06 '18 at 11:35
  • You can use any other character for na=" " in your write statement for NAs (depending how you plan to read it back). I checked my output and it has 20 lines – Katia May 06 '18 at 11:37
  • You might need to refresh the page to see the last part of the solution. As you can see I pad shorter vectors with NAs like this: waga_linkow[seq(selekcja)] – Katia May 06 '18 at 11:39
  • Can you save each result in a separate column and it will be great :) – Nerson48 May 06 '18 at 11:54
  • But this is what the write.csv() function does. Each vector saved in itsown column. Do you mean in its own row? – Katia May 06 '18 at 11:57
  • You just need to read the output in excel using comma as a separator. Normally it does it automatically, when you open file with *.csv extension, but you might need to adjust your format in excel – Katia May 06 '18 at 12:43
  • yes, it does. I added the screenshot of the output opened in excel – Katia May 06 '18 at 13:13
  • my excel does not separate cells, I have to do it manually is there any way? – Nerson48 May 06 '18 at 20:44
  • Try from menu: Data -> From Text. Then select the file and select "Delimited", press Next, select "comma" press "Finish" – Katia May 06 '18 at 20:49
  • so I did, but the next time you turn on the script, the settings reset – Nerson48 May 06 '18 at 21:06
  • Does the output text look correct if you open with regular notepad? Do you save file with csv extension? If everything looks good there, you need to figure out how to set Excel's properties correctly and this would be off the main question here. And I am not an excel specialist. Would any other output format work for you? Can you try to play with the separator? May be space or some other character would work better for your Excel. – Katia May 06 '18 at 21:26