CSV file output not well aligned with "read.csv()" - r

I run the "R code" in "RKWard" to read a CSV file:
# I) Go to the working directory
setwd("/home/***/Desktop/***")
# II) Verify the current working directory
print(getwd())
# III) Load te nedded package
require("csv")
# IV) Read the desired file
read.csv(file="CSV_Example.csv", header=TRUE, sep=";")
The data in CSV file is as follow (an example token from this website):
id,name,salary,start_date,dept
1,Rick,623.3,2012-01-01,IT
2,Dan,515.2,2013-09-23,Operations
3,Michelle,611,2014-11-15,IT
4,Ryan,729,2014-05-11,HR
5,Gary,843.25,2015-03-27,Finance
6,Nina,578,2013-05-21,IT
7,Simon,632.8,2013-07-30,Operations
8,Guru,722.5,2014-06-17,Finance
But the result is as follow:
id.name.salary.start_date.dept
1 1,Rick,623.3,2012-01-01,IT
2 2,Dan,515.2,2013-09-23,Operations
3 3,Michelle,611,2014-11-15,IT
4 4,Ryan,729,2014-05-11,HR
5 5,Gary,843.25,2015-03-27,Finance
6 6,Nina,578,2013-05-21,IT
7 7,Simon,632.8,2013-07-30,Operations
8 8,Guru,722.5,2014-06-17,Finance
PROBLEM: The datas are not aligned as supposed to be.
Please can anyone help me

Related

Clipboard isn't accessible

Here is my question:
I am trying my hand at datapasta(). I was able to do everything required but my finish result is not good.
How do I make my clipboard accessible so that I can copy what I need right into the clipboard instead of having to paste my copied text into the text box and then press saves to import my data? I believe this is the problem that is stopping me from properly viewing my copied data.frame correctly when I run head()
Here are the steps I followed via code chunk
install.packages("datapasta")
test<-data.frame(
stringsAsFactors = FALSE,
check.names = FALSE,
`Pos Team P W D L GD Pts` = c("1\tChelsea\t7\t5\t1\t1\t12\t16",
"2\tLiverpool\t7\t4\t3\t0\t15","3\tManchester City\t7\t4\t3\t0\t11\t14",
"4\tManchester United\t7\t4\t2\t1\t8\t14",
"5\tEverton\t7\t4\t2\t1\t5\t14",
"6\tBrighton\t7\t4\t2\t1\t3\t14","7\tBrentford\t7\t3\t3\t1\t4\t12",
"8\tTottenham\t7\t4\t0\t3\t12",
"9\tWest Ham\t7\t3\t2\t2\t4\t11",
"10\tAston Villa\t7\t3\t1\t3\t10","11\tArsenal\t7\t3\t1\t3\t10",
"12\tWolves\t7\t3\t0\t4\t9",
"13\tLeicester City\t7\t2\t2\t3\t8","14\tCrystal Palace\t7\t1\t4\t2\t7",
"15\tWatford\t7\t2\t1\t4\t7",
"16\tLeeds United\t7\t1\t3\t3\t6","17\tSouthampton\t7\t0\t4\t3\t4",
"18\tBurnley\t7\t0\t3\t4\t3",
"19\tNewcastle\t7\t0\t3\t4\t3","20\tNorwich City\t7\t0\t1\t6\t1")
)
head(test)
Here is my result which is not what I wanted
Pos\tTeam\tP\tW\tD\tL\tGD\tPts
1 1\tChelsea\t7\t5\t1\t1\t12\t16
2 2\tLiverpool\t7\t4\t3\t0\t15
3 3\tManchester City\t7\t4\t3\t0\t11\t14
4 4\tManchester United\t7\t4\t2\t1\t8\t14
5 5\tEverton\t7\t4\t2\t1\t5\t14
6 6\tBrighton\t7\t4\t2\t1\t3\t14
>
This is what pops up on my screen
Any help or suggestion will be greatly appreciated.

Download zip file to R when download link ends in '/download'

My issue is similar to this post, but the solution suggestion does not appear applicable.
I have a lot of zipped data stored an online server (B2Drop), that provides a download link with the extension "/download" instead of ".zip". I have been unable to get the method described here, to work.
I have created a test download page https://b2drop.eudat.eu/s/K9sPPjWz3jxtXEq, where the download link https://b2drop.eudat.eu/s/K9sPPjWz3jxtXEq/download can be obtained by right clicking the download button. Here is my script:
temp <- tempfile()
download.file("https://b2drop.eudat.eu/s/K9sPPjWz3jxtXEq/download",temp, mode="wb")
data <- read.table(unz(temp, "Test_file1.csv"))
unlink(temp)
When I run it, I get the error:
download.file("https://b2drop.eudat.eu/s/K9sPPjWz3jxtXEq/download",temp, mode="wb")
trying URL 'https://b2drop.eudat.eu/s/K9sPPjWz3jxtXEq/download'
Content type 'application/zip' length 558 bytes
downloaded 558 bytes
data <- read.table(unz(temp, "Test_file1.csv"))
Error in open.connection(file, "rt") : cannot open the connection
In addition: Warning message:
In open.connection(file, "rt") :
cannot locate file 'Test_file1.csv' in zip file 'C:\Users\User_name\AppData\Local\Temp\RtmpMZ6gXi\file3e881b1f230e'
which typically indicates a problem with the working directory where R is looking for the file. In this case that should be the temp wd.
Your internal path is wrong. You can use list=TRUE to list the files in the archive, analogous to the command-line utility's -l argument.
unzip(temp, list=TRUE)
# Name Length Date
# 1 Test/Test_file1.csv 256 2021-09-27 10:13:00
# 2 Test/Test_file2.csv 286 2021-09-27 10:14:00
Better than read.table, though, use read.csv since it's comma-delimited.
data <- read.csv(unz(temp, "Test/Test_file1.csv"))
head(data, 3)
# ID Variable1 Variable2 Variable Variable3
# 1 1 f 54654 25 t1
# 2 2 t 421 64 t2
# 3 3 x 4521 85 t3

Importing csv to R, extract polygon and decode it with googlePolylines packages

I have been trying to decode encrypted google polygons with R using the googlePolylines package which I have achieved by setting the polygon as a text but cannot do the same procedure by importing a csv file with the polygons.
For example, I have the following code:
library(googlePolylines)
pol<-"j~thAxdvtMtXmL|LgCpKxCjQzD~T`GzXrAlMePnDsKpVoF`QgBnNoM~IcA`JiGnd#iYvZuLzKkSlH_d#wDef#iJwFyZfUmc#hCsa#N{\\|SeCzOaFgQwD{SaPiC|WwBpPr#bGsG{FeQxEkOtXa#dXgFtHyJoIiGui#NqaAvFuRyQxEia#he#u_#b\\og#dHuZyJaX_d#zK_Ua#eBwMq`A}AyRcTkKce#c]uWeS{DuRzZtCh]|b#zGuCbm#fUdTsLbS__#wBoTlu#zVbm#dc#l]oExn#gf#vUgPbAkL|HtCnXi#zKtRvc#CzPxBpLxAnEvFvDpPx^cFlBTh#??Dn##f#~G|QFH~\\gMj#Su#}#"
polylines<-c(pol)
coordinates<-decode(polylines)
This works but for this I have to change manually the encrypted polygon. What I want to do is to import a csv (datapolygons.csv) with the encrypted polygon (column polygon) and then decode it. I have tried with the following code but it gives me an error message.
datapolygons.csv (polygon is written the same as the previous encoded polygon)
store_id | store_name | polygon
12345 | RANDOM | "j~thAxdvtMtXmL....."
library(googlePolylines)
polyfile=read.table('datapolygon.csv', header=TRUE, sep=',')
pol<-levels(droplevels(polyfile$polygon))
polylines<-c(pol)
coordinates<-decode(polylines)
Error message:
Error in rcpp_decode_polyline(polylines, "coords") :
basic_string::at: __n (which is 363) >= this->size() (which is 363)
Any help would be greatly appreciated. Thank you!
Is it possible the string is corrupt in your .csv file? Perhaps when you made the data.frame to write out? The corruption seems to occur during conversion to factor().
I have no trouble writing it out and back in:
polyfile <- data.frame(store_address_id = 12345,
store_name = "RANDOM",
polygon = "j~thAxdvtMtXmL|LgCpKxCjQzD~T`GzXrAlMePnDsKpVoF`QgBnNoM~IcA`JiGnd#iYvZuLzKkSlH_d#wDef#iJwFyZfUmc#hCsa#N{\\|SeCzOaFgQwD{SaPiC|WwBpPr#bGsG{FeQxEkOtXa#dXgFtHyJoIiGui#NqaAvFuRyQxEia#he#u_#b\\og#dHuZyJaX_d#zK_Ua#eBwMq`A}AyRcTkKce#c]uWeS{DuRzZtCh]|b#zGuCbm#fUdTsLbS__#wBoTlu#zVbm#dc#l]oExn#gf#vUgPbAkL|HtCnXi#zKtRvc#CzPxBpLxAnEvFvDpPx^cFlBTh#??Dn##f#~G|QFH~\\gMj#Su#}#",
stringsAsFactors = FALSE)
write.csv(file="~/test.csv",polyfile)
polyfile2 <- read.table('~/test.csv', header=TRUE, sep=',',stringsAsFactors = FALSE)
decode(polyfile2$polygon)
[[1]]
lat lon
1 -12.07286 -76.95965
2 -12.07697 -76.95750
3 -12.07920 -76.95682
4 -12.08121 -76.95759
5 -12.08415 -76.95853
6 -12.08767 -76.95982
7 -12.09181 -76.96024
8 -12.09412 -76.95749
9 -12.09500 -76.95547
10 -12.09877 -76.95427

incomplete list of csv file imported in R

I need to import a list of 36 csv files, but after running the code I get only 26 of them. Probably, 10 files have format problems. Is there a way in R to detect the 10 files that cannot be imported?
If you the file names in a list, you can use the following code:
all <- c("16048.txt", "16062.txt", "16066.txt", "16093.txt", "16095.txt", "16122.txt", "16241.txt", "16360.txt", "16380.txt", "16389.txt", "16510.txt", "16511.txt", "16701.txt", "16729.txt", "16735.txt", "16737.txt", "16761.txt", "16816.txt", "16867.txt", "16876.txt", "16880.txt", "16883.txt", "16884.txt", "16885.txt", "16893.txt", "16904.txt", "16906.txt", "16908.txt", "16929.txt", "16931.txt", "16938.txt", "16943.txt", "16959.txt", "16967.txt", "16968.txt", "16969.txt")
imp <- c("16761.txt", "16959.txt", "16884.txt", "16093.txt", "16883.txt", "16122.txt", "16906.txt", "16737.txt", "16968.txt", "16095.txt", "16062.txt", "16816.txt", "16360.txt", "16893.txt", "16885.txt", "16938.txt", "16048.txt", "16931.txt", "16876.txt", "16511.txt", "16969.txt", "16241.txt", "16967.txt", "16701.txt", "16380.txt", "16510.txt")
Where all is the list of filenames you need and imp is the imperfect result you got. You can get a list of the missing files with:
missing <- all[!all %in% imp]

rDrop dropbox api non-expiring tokens/seamless token issues

I am using the rDrop package that is available from https://github.com/karthikram/rDrop, and after a bit of tweaking (as all the functions don't quite work as you would always expect them to) I have got it to work finally in the way I would like, but it still requires authorisation verification to allow the use of the app, once you get the token each time, as I think that tokens expire over time...(if this is not the case and I can hard code in my token please tell me as that would be a good solution too...)
Basically I wanted a near seamless way of downloading csv files from my dropbox folders from the commandline in R in one line of code so that I dont need to click on the allow button after the token request.
Is this possible?
Here is the code I used to wrap up a dropbox csv download.
db.csv.download <- function(dropbox.path, ...){
cKey <- getOption('DropboxKey')
cSecret <- getOption('DropboxSecret')
reqURL <- "https://api.dropbox.com/1/oauth/request_token"
authURL <- "https://www.dropbox.com/1/oauth/authorize"
accessURL <- "https://api.dropbox.com/1/oauth/access_token/"
require(devtools)
install_github("ROAuth", "ropensci")
install_github("rDrop", "karthikram")
require(rDrop)
dropbox_oa <- oauth(cKey, cSecret, reqURL, authURL, accessURL, obj = new("DropboxCredentials"))
cred <- handshake(dropbox_oa, post = TRUE)
raw.data <- dropbox_get(cred,dropbox.path)
data <- read.csv(textConnection(raw.data), ...)
data
}
Oh and if its not obvious I have put my dropbox key and secret in my .Rprofile file, which is what the getOption part is referring to.
Thanks in advance for any help that is provided. (For bonus points...if anybody knows how to get rid of all the loading messages even for the install that would be great...)
library(rDrop)
# my keys are in my .rprofile, otherwise specifiy inline
db_token <- dropbox_auth()
# Hit ok to authorize once through the browser and hit enter back at the R prompt.
save(db_token, file="my_dropbox_token.rdata")
Dropbox token are non-expiring and can be revoked anytime from the Dropbox web panel.
For future use:
library(rDrop)
load('~/Desktop/my_dropbox_token.rdata')
df <- data.frame(x=1:10, y=rnorm(10))
> df
x y
1 1 -0.6135835
2 2 0.3624928
3 3 0.5138807
4 4 -0.2824156
5 5 0.9230591
6 6 0.6759700
7 7 -1.9744624
8 8 -1.2061920
9 9 0.9481213
10 10 -0.5997218
dropbox_save(db_token, list(df), file="foo", ext=".rda")
rm(df)
df2 <- db.read.csv(db_token, file='foo.rda')
> df2
x y
1 1 -0.6135835
2 2 0.3624928
3 3 0.5138807
4 4 -0.2824156
5 5 0.9230591
6 6 0.6759700
7 7 -1.9744624
8 8 -1.2061920
9 9 0.9481213
10 10 -0.5997218
If you have additional problems, please file an issue.

Resources