Writing dataframe to dbf file in R - r

I'm looking to export a dataframe as a dbf file in R, and am using write.dbf but I get an error that says:
Error in write.dbf(mydata, "mydata.dbf") :
could not find function "write.dbf"
I've tried several variations of the filename portion for write.dbf (i.e no quotations, no extension, with the path included) but the all say the function can't be found.
Why is this the case?

Try this:
install.packages("foreign")
library(foreign)
write.dbf(mydata, "mydata.dbf")

As commented, you should first load the package containing this function. if you don't remember which package the function is from, you might retrieve it using ??write.dbf

Related

R error in fwrite: name of file to write is an invalid argument?

I've got a long code in R where I want to write a csv file multiple times. I use fwrite for this. The code has worked in the past, but now gives an error:
fwrite(IDs, "IDS.csv")
Error in fwrite(IDs, "IDS.csv") : Invalid argument: 'IDS.csv'
IDs.csv is supposed to be the name of the csv file written, I don't see how this can be an invalid argument? Adding file= doesn't solve the problem.
I can use write.csv instead but the fwrite function is used a lot in this code and I don't want to have to change all of them.

write r file to csv -- "object not found"

Hello I need to make my R file into a CSV file. I have searched stackoverflow for different methods to accomplish this, but nothing is working. Each time, it says:
Error in is.data.frame(x) : object 'refine_clean' not found
My file is definitely named refine_clean.R. I have linked a screenshot here:
R Studio CSV Error Message
Your file is named refine_clean but you have no data.frame or object named refine_clean, which is what write_table and write.csv are looking for.
based on the screen shot, it is not clear what you want to produce. If it is the last object you created (d5), you may want something like this
write.csv(d5, "refine_clean.csv")
you cant convert all your file as an csv, you need to select the object, like d5
write.csv(d5,file = "name_of_the_file")

PDF File Import R

I have multiple .pdf-files (stored in a local folder), that contain text. I would like to import the .pdf-files (i.e., the texts) in R. I applied the function 'read_dir' (R package: [textreadr][1])
library ("textreadr")
Data <- read_dir("<MY PATH>")
The function works well. BUT. For several files, that include special characters (i.e., letters) in their names (such as 'ć'; e.g., 'filenameć.pdf'), the function did not work (error message: 'The following files failed to read in and were removed:' …).
What can I do?
I tried to rename the files via R (did not work (probably due to the same reasons)). That might be a workaround.
I did not want to rename the files manually :)
Follow-Up (only for experts):
For several files, I got one of the following error messages (and I have no idea why):
PDF error: Mismatch between font type and embedded font file
or
PDF error: Couldn't find trailer dictionary
Any suggestions or hints how to solve this issue?
Likely the issue concerns the encoding of the file names. If you absolutely want to use R to rename the files for you, the function you want to use is iconv, determine the encoding of the file names and then convert them to utf-8.
However, a much better system would imply renaming them using bash from command line. Can you provide a more complete set of examples?

How to read .xlsx file using XLConnect in R

I want to read an .xls or .xlsx file from my hard drive using R. I installed the XLConnect package and have received the following errors:
Data <- readWorksheet(loadWorkbook("C:/test1.xlsx"),sheet=1)
Error: FileNotFoundException (Java): File 'test1.xlsx' could not be found - you may specify to automatically create the file if not existing.
I want to read the first tab of my Excel file. I also tried the gdata read.xls function and failed.
Try to define your working directory before calling the xlsx file. So use the function setwd before calling the file. Example:
setwd("the location where the file is placed on your pc")
Data <- readWorksheet(loadWorkbook("C:/test1.xlsx"),sheet=1)
Note: make sure u are using forward slashes instead of backwards slashes in the setwd function.

How to export a dataset to SPSS?

I want to export a dataset in the MASS package to SPSS for further investigation. I'm looking for the EuStockMarkets data set in the package.
As described in http://www.statmethods.net/input/exportingdata.html, I did:
library(foreign)
write.foreign(EuStockMarkets, "c:/mydata.txt", "c:/mydata.sps", package="SPSS")
I got a text file but the sps file is not a valid SPSS file. I'm really looking for a way to export the dataset to something that a SPSS can open.
As Thomas has mentioned in the comments, write.foreign doesn't generate native SPSS datafiles (.sav). What it does generate is the data in a comma delimited format (the .txt file) and a basic syntax file for reading that data into SPSS (the .sps file). The EuStockMarkets data object class is multivariate time series (mts) so when it's exported the metadata is lost and the resulting .sps file, lacking variable names, throws an error when you try to run it in SPSS. To get around this you can export it as a data frame instead:
write.foreign(as.data.frame(EuStockMarkets), "c:/mydata.txt", "c:/mydata.sps", package="SPSS")
Now you just need to open mydata.sps as a syntax file (NOT as a datafile) in SPSS and run it to read in the datafile.
Rather than exporting it, use the STATS GET R extension command. It will take a specified data frame from an R workspace/dataset and convert it into a Statistics dataset. You need the R Essentials for Statistics and the extension command, which are available via the SPSS Community site (www.ibm.com/developerworks/spssdevcentral)
I'm not trying to answer a question that has been answered. I just think there is something else to complement for other users looking for this.
On your SPSS window, you just need to find the first line of code and edit it. It should be something like this:
"file-name.txt"
You need to find the folder path where you're keeping your file:
"C:\Users\DELL\Google Drive\Folder-With-Your-File"
Then you just need to add this path to your file's name:
"C:\Users\DELL\Google Drive\Folder-With-Your-File\file-name.txt"
Otherwise SPSS will not recognize the .txt file.
Sorry if I'm repeating some information here, I just wanted to make it easier to understand.
I suppose that EuStockMarkets is a (labelled) data frame.
This should work and even keep the variable and value labels:
require(sjlabelled)
write_spss(EuStockMarkets, "mydata.sav")
Or you try rio:
rio::export(EuStockMarkets, "mydata.sav")

Resources