Pågår
avsnitt 9 av 17
Pågår

Exporting (saving) data

Let’s create a data frame manually. The data frame will contain 3 variables (columns), which will be created separately, like so:

# Create a number sequence from 1 to 10
variable1 <- 1:10

# Create 10 randomly generated numbers using the rnorm() function
variable2 <- rnorm(10)

# Create a character vector
variable3 <- c("A", "B", "W", "C", "D", "E", "A", "B", "C", "D")

# Combine them into a data frame using the data.frame() function
my_data_frame <- data.frame(variable1, variable2, variable3)

# View the data frame
my_data_frame
##    variable1  variable2 variable3
## 1          1 -1.1261992         A
## 2          2 -0.7705238         B
## 3          3  0.2431920         W
## 4          4  0.6924714         C
## 5          5 -1.3598631         D
## 6          6 -1.5128399         E
## 7          7  0.1945323         A
## 8          8 -0.8373292         B
## 9          9 -1.0180647         C
## 10        10 -1.9714896         D

Next, we create a new variable, variable4, which is the product of variable1 and variable2:

my_data_frame$variable4 <- my_data_frame$variable1 * my_data_frame\$variable2

# View the data frame
my_data_frame
##    variable1  variable2 variable3   variable4
## 1          1 -1.1261992         A  -1.1261992
## 2          2 -0.7705238         B  -1.5410475
## 3          3  0.2431920         W   0.7295759
## 4          4  0.6924714         C   2.7698857
## 5          5 -1.3598631         D  -6.7993157
## 6          6 -1.5128399         E  -9.0770394
## 7          7  0.1945323         A   1.3617262
## 8          8 -0.8373292         B  -6.6986335
## 9          9 -1.0180647         C  -9.1625826
## 10        10 -1.9714896         D -19.7148959

Now that all is done and great, we want to save our data frame. As discussed earlier, you should always prefer to use CSV format for import and export, since this will assure the best compatibility with other software. We will use the function write.csv() to write our data frame to a new CSV file, like so:

write.csv(my_data_frame, file = "my_saved_data_frame.csv", row.names = FALSE)

R will save the data frame my_data_frame to a CSV file called my_saved_data_frame.csv. The write.csv() function has many arguments, with which you can customize your file. The two first arguments in the example above are mandatory. The second argument (row.names = FALSE) is optional byt highly recommended.

• The first argument is the name of the data frame that you wish to save (my_data_frame).
• The second argument is the file name to give your exported file (you must also include the file extension).
• The third argument row.names = FALSE tells R not to add a column of numbers (row numbers) as the very first column in the saved file.

The file will be saved to your current working directory. If you don’t remember your current working directory, simply execute the following command, which will return the working director:

getwd()
[1] "/Desktop/Data Science/"

So the file will be saved to the folder Data Science, which is located on my Desktop. If you prefer to save your file elsewhere, you have two options:

Option 1: change the working directory

# Change working directory to OtherFolder on my Desktop
setwd("/Desktop/OtherFolder/")

# Save file
write.csv(my_data_frame, file = "my_saved_data_frame.csv", row.names = FALSE)

Option 2: Provide exact path to location

# Save file
write.csv(my_data_frame, file = "/Desktop/OtherFolder/my_saved_data_frame.csv", row.names = FALSE)