I'm getting an error loading csv file onto Jupyter Notebook having specified the below filepath:
df = pd.read_csv(r"C:\Users\bino\Desktop\sale_data.csv")
Can anyone suggest an alternative please?
you need only to use proper syntax:
df = pd.read_csv("C:\Users\bino\Desktop\sale_data.csv")
if still it not working then try other slashes in path like / or // or \ or \\
in your path instead of ' \ '
Related
I am trying to change a R script file to a lowercase r file extension? My script file at looks like this when created on the MacOS version of RStudio.
Test_File.R
I have read that this can cause a permission denied error. Is there an easy way to change this to a lowercase r file extension? Such as...
Test_File.r
I have tried renaming the file extension for 'R' to 'r' in Finder but I am not sure whether renaming the file extension to 'r' in Finder is enough?
R can do it the following way:
x <- list.files(pattern = "\\.R$") # make sure to set working directory first
file.rename(x, sub("R$", "r", x))
Save these all command into a text.file suppose you save in (rename_shell_script)
ls *R > list
cp list list1
sed -i 's/.R/.r/g' list1
paste list list1| sed 's/^/mv /g' |sh
run it like :
$sh rename_shell_script
for linux terminal users: #make sure you are in working directory first
rename 's:(.*)\.R:$1.r:g' *.R
Use filename in place of * if you want to make change in the single file only.
I am trying to import data from excel into R (stats class), but we are not allowed to use the import function. We have to code it in. I got help last week from a graduate student, but I think because I got a new laptop over the weekend, the Rmd file cannot be used as my reference markdown anymore. Here is what I've tried inputting:
library(tidyverse)
data.frame(readxl::read_excel(path = "./data/thedataweprovide.xlsx", sheet = "Problem 1"))
Error: `path` does not exist: ‘./data/thedataweprovide.xlsx’
> setwd("C:/Users/Jasmine Ferrell/Documents/BME_Statistical_Methods/Project_1")
Error in setwd("C:/Users/Jasmine Ferrell/Documents/BME_Statistical_Methods/Project_1") :
cannot change working directory
> pone
Error: object 'pone' not found
> setwd("C:/Users/Jasmine Ferrell/Documents/BME_Statistical_Methods/Project_1")
Error in setwd("C:/Users/Jasmine Ferrell/Documents/BME_Statistical_Methods/Project_1") :
cannot change working directory
> setwd("C:\Users\ferre\OneDrive\Documents\BME_Statistical_Methods\Project_1")
Error: '\U' used without hex digits in character string starting ""C:\U"
> setwd(C:/Users/ferre/OneDrive\Documents/BME_Statistical_Methods/Project_1)
Error: unexpected '/' in "setwd(C:/"
It looks like your paths are the problem. You could first try to get the working directory you are currently in with the function getwd() this may help you to figure out how you could correct your paths. Do not use single Backslashes (\) in paths, only use foreward-slashes (/).
As saae mentioned its a problem with path. Try these and should be able to get it working.
Type in your Rstudio
getwd()
This gives the current path which is currently set. If you are files are somewhere else, then you won't find it.
To change the path
go to your file location and copy the path. For example in windows, you go to the folder and click on properties, there you will see location. Copy the location and add it to the below code.
setwd("//abcdefg/somerandomguy/Profile/Desktop/My files/R_project")
Then you can run your code to read the files.
I am trying to provide a way of running unattended R scripts through Rscript. The problem I am having is that the default output for graphics is a PDF file in the current directory. I would like to redirect this output to a separate folder but without having to change the script too much.
If my script is something simple as:
args = commandArgs(trailingOnly = TRUE)
mydata <- get some data frame somehow
plot(tayside)
And I execute the following commandline:
Rscript.exe --vanilla --default-packages=RODBC,graphics,grDevices sample.R > C:\temp\sample.Rout
I get a Rplots.pdf in the current folder and the sample.Rout file in the C:\temp\ folder.
Is there a way to specify an output folder and have Rscript put all output there? I have tried playing with the pdf.options(...) to pre-pend a default folder to the file parameter but no can do.
Ok, apparently it was easier than I thought, no need to use pdf.options() but simply pdf() at the top of the file (after getting the arguments):
pdf(paste0(args[1], "MyFile.pdf"))
or, for multiple files:
pdf(paste0(args[1], "MyFile%03d.pdf"), onefile=FALSE)
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")
I have a wget_string and commands made as follows:
wget_string <- paste("wget --user=" , u_name , " --password=", p_word, " ", my_urls,' -qO ', file_name, sep="")
system(wget_string)
readLines(file_name)
Which works, but then I have to use readLines() to read the file into R. I would like to run the command so that file is available directly in R, without being saved to the hard disk and then loaded from it.
I'm hoping to save resources by loading the file straight into R from the web. Can't use readlines from the beginning because of the secure server. What are the options for this?
An intern=TRUE parameter the system function has. Capture the output from the command it does. Use the force, and the right options to wget to print to stdout:
> wget_string="wget -qO- http://www.google.com"
> s = system(wget_string,intern=TRUE)
If your returned data is a CSV file, textConnection you can use, feed it to read.csv you can.
If you use the -O - option of wget you set the output to the standar output (writing directly on the screen). In this way you can read directly from the output of the wget command.
E.g.
wget -O - http: //www.address.com
Will download the web page and print it directly to the standard output. So you can directly read the output of the system(wget_string).
From the wget man page:
-O file
--output-document=file
The documents will not be written to the appropriate files, but all will be concatenated together and written to file. If - is used as file, documents will be printed to standard output, disabling link conversion. (Use
./- to print to a file literally named -.)