error received while converting a txt to csv - r

I am attempting to convert a text file to a csv file using R because it is too big to open as a text file normally. The source file is space separated.
my current code is simply:
data<-read.table("name.txt")
x<-as.data.frame(data)
write.csv(x,file="name2.csv")
The code works fine when I use another file, but when I use my target file I get the error code:
Error in scan(file, what, nmax,sep,dec,quote,skip,nlines,na.strings, :line 1 did not have 24 elements
What would cause this issue and how do I resolve it?

Related

How to fix "Error: `path` does not exist:" and "Contains magic number?"

I'm trying to read a .xls file but it is not working. It either says that the file-path does not exist, that the file is corrupted or that it contains a magic number. When manually selecting the file it also does not work, but I can open the excel file just fine.
I was initially trying to convert the xls file to CSV and reading it using read.csv, then I started using read_excel directly with the xls file
load("~/Downloads/Crime2018EXCEL/noncampusarrest151617.xls")
Error in load("~/Downloads/Crime2018EXCEL/noncampusarrest151617.xls") :
bad restore file magic number (file may be corrupted) -- no data loaded
In addition: Warning messages:
1: In grepl("RD[ABX][2-9]\n", magic) :
input string 1 is invalid in this locale
2: In grepl("RD[ABX][2-9]\r", magic) :
input string 1 is invalid in this locale
3: file ‘noncampusarrest151617.xls’ has magic number '������������������������'
Use of save versions prior to 2 is deprecated
crimestat<-read_xls("oncampuscrime151617.xls") #You might need to change the filepath
Error: path does not exist: ‘oncampuscrime151617.xls’

Error when using read.table

I need help to put a .txt file into R. I'm trying to input a text document into R for a stats class but it isn't working.
I put it in read.table("TransaniaIncomes.txt", header=TRUE) but the following message keeps coming up:
Error in file(file, "rt") : cannot open the connection In addition:
Warning message: In file(file, "rt") : cannot open file
'TransaniaIncomes.txt': No such file or directory
I don't really know what to do. The file extends for several thousand characters like this in 5 straight columns:
3.755556 13.51542 14.4545 19.93651 21.62806
57.48426 7.282404 38.48386 10.22754 9.9399
21.38976 6.675126 32.14614 53.96588 12.37087
30.35346 10.20072 2.650464 26.84042 14.13381
I. getwd()
Make sure your .txt file is in the current working directory of RStudio. To check current working directory of RStudio, type in console: getwd() and see if your file exists in that path.
II. setwd()
If your file exists in some other path, you can set that path in RStudio using setwd()and then run the read.table() command. Example:
setwd("C:/My/Path/To/TextFile") # The .txt file would be in TextFile folder
read.table("TransaniaIncomes.txt", header=TRUE)
III. file.choose()
To avoid path setting and you know where your file is stored, you can use file.choose() function inside read.table() which will open up a interactive dialog box to go and select your .txt file from PC, wherever it is.
read.table(file.choose(), sep="\t", header=TRUE) # Choose "TransaniaIncomes.txt"
Note: file.choose() would open a dialog box to choose file from you PC from any path.
If your txt file is tab separated, you can include sep = "\t" argument to read.table.
header=TRUE would come if you have column names in your text file, if not then it should be set to FALSE.
R told you that he cannont find your file. you have 2 options:
1) type getwd() in the terminal. It will show your path to the working directory. So put your file in that folder and it should work.
2) find the path of your file ex C:/doc/TransaniaIncomes.txt add the path to your read.table() function.
read.table("C:/doc/TransaniaIncomes.txt", header=TRUE)
tips: Make sure your file name is written correctly.

r function unzip error 1 in extracting from zip file

Environment:
Windows 7 OS
RStudio Version 0.99.491
I have been programming in R for about 4 months via the Coursera Data Science curriculum, but I have NEVER been successful in using the unzip function.
I've looked at the forums for hours for potential solutions, syntax problems, undefined arguments, etc., but to no avail. I eventually unzip the contents manually and proceed with the assignment, but I am tired of not knowing why it is not working.
Here are a few examples of the error:
fileName <- "StormData.zip"
unzip(fileName, exdir = mainDir,subDir)
Warning message: In unzip(fileName, exdir = mainDir, subDir) : error
1 in extracting from zip file
unzip(fileName)
Warning message: In unzip(fileName) : error 1 in extracting from zip
file
unzip(fileName, "stormdata.csv")
Warning message: In unzip(fileName, "stormdata.csv") : error 1 in
extracting from zip file
unzip(fileName, "stormdata.csv", list = TRUE)
Error in unzip(fileName, "stormdata.csv", list = TRUE) : zip file
'StormData.zip' cannot be opened
Any suggestions would be greatly appreciated.
I was getting the same error.
I changed the path --
from :
uzp <- "C:\\Users\\Sharvari\\Downloads\\rprog%2Fdata%2Fspecdata"
to
uzp <- "C:\\Users\\Sharvari\\Downloads\\rprog%2Fdata%2Fspecdata.zip"
and it works fine!
setwd("C:\\Users\\Sharvari\\Downloads")
uzp <- "C:\\Users\\Sharvari\\Downloads\\rprog%2Fdata%2Fspecdata.zip"
unzip(uzp, exdir = "C:\\Users\\Sharvari\\Desktop\\specdata")
I too was getting that error 1 message when trying to unzipping a zip file. Glitch in my case was the conflict between working directory and zip file path.
My case was:
My working directory was like "C:/Users/SCOTT/Desktop/Training"
While my zip file was located in "C:/Users/SCOTT/Desktop/Training/house_consumption_data"
When I was trying to execute this:
unzip("house_data.zip")
Possibly your file is in a different folder.
I have had the same problem trying to download and unzip the same file, for the same course. And I have had problems with unzip in the past and was determined to solve it this time too.
Eventually the extension of the file turned out to be csv.bz2. And than this Extract bz2 file in R post solved my problem.
After downloading the file I was able to read it directly with
stormdata <- read.csv("stormdata.zip")
without using unzip.
This error seems to appear whenever openXLS is unable to open the specified file.
It could be a wrong name, wrong directory or the file might be encrypted or password protected
change your zip file format this error will appear while the zip format problems occur, look at your zip file it should be "rar" change it to "zip". the function works only for "zip" format files.
I faced the same issue. Make sure that, you specify the correct name of the file(get it from the properties of .zip file) in the following code.
file = read.table(unzip("file_name.csv.zip"), sep = ",", header = TRUE)
In my case, Was just mentioning file_name.zip and R was throwing the error.
Also, there are two functions for unzipping files in R
1) unz - to extract single element from zip file/s
2) unzip - to extract all the present elements from the .zip file
I usually prefer unzip.
If you will use unz in the above code, R will throw error again.
I encountered the same error using install_course_zip' with a zip file. I followed all the instructions for the command faithfully but kept getting errors relating to the 'exdir'. I moved the zip file to various directories without success.
I finally used getwd() to get the working directory and then placed the zip file in that directory. I then was able to use the zip file name without having to use any folder structure and this worked. I still have no idea why R would not accept a different directory.
I had list of files to be unzipped and processed; I was facing same error
"error 1 in extracting from zip file"
used full directory and set working directory code worked
files <- list.files(path="C:\\Users\\Tejas naik\\Documents\\land", pattern=".zip$")
out_dir<- "C:\\Users\\Tejas naik\\Documents\\input"
setwd("C:\\Users\\Tejas naik\\Documents\\land")
for (i in files) {
#nzip(paste(out_dir,i), exdir=out_dir)
unzip(i,exdir=out_dir)
}
This error was happening bit differently in my case . As there was no zip file ,the issue was file was open in excel so this error was poping up .
It's crucial to give the full name (including the path) of the zip-file to the unzip function.
So instead of file.zip, it should be C:\user\name\file.zip.
In case you're using the list.files function, one should set the full.names option to TRUE.
For me the error is fixed after I add \ backslash character to the filepath.
Example:
from
unzip("abc\aaa.zip")
to
unzip("abc\\aaa.zip")

Unexpected input error in Shiny, but unable to locate the source of error

I am getting the unexpected input error in UI.R, as follows:
ERROR: C:\Users\myApp/ui.R:1:2: unexpected input
1: ï»
However, when I try to locate the error at line 1, there is absolutely nothing of the form ï».
To resolve this error, I tried saving my UI.R file as a text file and changing the encoding to UTF-8, but this still does not remove the strange character. I also tried removing the first couple of lines and re-writing the code, but it still gives the same error!
How can I remove this character? Should I use another text editor?
I am using base R, not R Studio. And I had copy-pasted my code form my GitHub account, if that info is required...
Code from my file can be viewed here.
Many thanks.
I have this same issue in the year '19, and took me a while to run into this question from the year '14.
Not Shiny, but a regular R project with its .Rprofile.
The solution that worked for me is:
Open your file in Notepad++. From the Encoding menu, select Convert to UTF-8 (without BOM), save the file, replace the old file with this new file. And all is fixed.

How do I read in a CSV file into R?

I'm having some technical issues with loading a CSV file into R. When I inspect the csv file in RStudio's Source pane, all the characters are surrounded by weird red circles or dots. When I inspect another self-made CSV file, the characters appear perfectly fine, without any of the red circles.
What is this issue/symptom, and what would be the best way to fixing this for about 40 similar CSV files?
When I try to run readfile <- read.csv("filename.csv", sep="", collapse=NULL) I get the following error:
Error in read.table(file = file, header = header, sep = sep, quote = quote, :
empty beginning of file
My guess would be that you ran into some encoding issue.
Especially on Windows you can run into all sorts of problems with that.
Try opening the csv file with a text editor that has the capability of saving files with various Encodings (e.g. Notpead++) then change that to e.g. UTF-8 (which is the preferred Encoding of RStudio most other Editors and R itself), save the file and try to run the import again.
Just make sure that you don't loose characters - especially special characters tend to get lost during Encoding changes.
Greetings ...

Resources