Course
Data Frames
Convert a List to a DataframeCreate an Empty DataframeCombine Two Dataframe into OneChange Column Name of a DataframeExtract Columns From a DataframeDrop Columns in a DataframeReorder Columns in a DataframeSplit DataframeMerge Multiple DataframesDelete Rows From DataframeMake a List of DataframesIntroduction
"Hello World" ProgramAdd Two VectorsFind Sum, Mean and Product of Vector in R ProgrammingTake Input From UserGenerate Random Number from Standard DistributionsSample from a PopulationFind Minimum and MaximumSort a VectorStrings
Concatenate Two StringsFind the Length of a StringCheck if Characters are Present in a StringExtract n Characters From a StringReplace Characters in a StringCompare two StringsConvert Factors to CharactersTrim Leading and Trailing WhitespacesVectors
Concatenate a Vector of StringsCheck if a Vector Contains the Given ElementCount the Number of Elements in a VectorFind Index of an Element in a VectorAccess Values in a VectorAdd Leading Zeros to VectorR Program to Write to a File
Example 1: Write a Sentence to txt File in R
sentence1 <- "Data Science is fun"
# use writeLines() to write to txt file
writeLines(sentence1, "file1.txt")
In the above example, we have used the writeLines()
function to write the string named sentence1 to a txt file. Notice the arguments passed inside writeLines()
,
writeLines(sentence1, "file1.txt")
Here,
- sentence1 - name of the string we want to export
file1.txt
- name of the txt file
Our file1.txt
would look like this:
Note: If the file is in some other location, we have to specify the path along with the file name as: writeLines("D:/folder1/file1.txt")
.
Example 2: Write Into CSV File in R
# Create a data frame
dataframe1 <- data.frame (
Name = c("Juan", "Alcaraz", "Simantha"),
Age = c(22, 15, 19),
Vote = c(TRUE, FALSE, TRUE))
# write dataframe1 into file1 csv file
write.csv(dataframe1, "file1.csv")
In the above example, we have used the write.csv()
function to export a dataframe named dataframe1 to a CSV file. Notice the arguments passed inside write.csv()
,
write.csv(dataframe1, "file1.csv")
Here,
- dataframe1 - name of the data frame we want to export
file1.csv
- name of the csv file
Finally, the file1.csv
file would look like this in our directory: