[R] Error in reading subset of data from CSV file
Florian Schwendinger
Florian_Schwendinger at gmx.at
Mon Sep 5 19:58:50 CEST 2016
There are several issues,
1. I think
ReadName = '133261'
read.csv.sql("Dat.csv", sql = "select * from file where 'col 1' = ReadName")
should be replaced by something like this
ReadName = '133261'
sql_cmd <- sprintf("select * from file where col_1='%s'", ReadName)
sql_cmd
read.csv.sql("Dat.csv", sql = sql_cmd, sep="|")
2. I wouldn't use spaces in the column names.
3. Since you have in column 2 also your separator, you cloud switch the
separator to "|" or ";" if possible.
colnames(Dat) <- gsub("\\s+", "_", colnames(Dat))
colnames(Dat)
write.table(Dat, "Dat.csv", quote = FALSE, row.names = FALSE, sep="|")
ReadName = '133261'
sql_cmd <- sprintf("select * from file where col_1='%s'", ReadName)
sql_cmd
read.csv.sql("Dat.csv", sql = sql_cmd, sep="|")
So the following works at my pc, but if you cant change the separator
things will get more difficult.
colnames(Dat) <- gsub("\\s+", "_", colnames(Dat))
colnames(Dat)
write.table(Dat, "Dat.csv", quote = FALSE, row.names = FALSE, sep="|")
ReadName = '133261'
sql_cmd <- sprintf("select * from file where col_1='%s'", ReadName)
sql_cmd
read.csv.sql("Dat.csv", sql = sql_cmd, sep="|")
Best,
Florian
On 2016-09-04 13:29, Christofer Bogaso wrote:
> Didnt work.... getting unused argument error.
>
> On Sun, Sep 4, 2016 at 4:47 PM, Jim Lemon <drjimlemon at gmail.com> wrote:
>> I suppose you could try quote=TRUE
>>
>> Jim
>>
>>
>> On Sun, Sep 4, 2016 at 8:13 PM, Christofer Bogaso
>> <bogaso.christofer at gmail.com> wrote:
>>> Thanks Jim. But my data is like that and I have to live with that. Any
>>> idea on workaround. Thanks,
>>>
>>> On Sun, Sep 4, 2016 at 3:40 PM, Jim Lemon <drjimlemon at gmail.com> wrote:
>>>> Hi Christofer,
>>>> You have embedded commas in your data structure. This is guaranteed to
>>>> mess up a CSV read.
>>>>
>>>> Jim
>>>>
>>>>
>>>> On Sun, Sep 4, 2016 at 5:54 PM, Christofer Bogaso
>>>> <bogaso.christofer at gmail.com> wrote:
>>>>> Hi again,
>>>>>
>>>>> I was trying to read a subset of Data from a CSV file using below code
>>>>> as example :
>>>>>
>>>>> library(sqldf)
>>>>>
>>>>> Dat = structure(list(`col 1` = c(133261L, 133261L, 133262L, 133262L
>>>>> ), `col 2` = structure(1:4, .Label = c("aaa1", "aaa2", "bbb1, bbb",
>>>>> "bbb3, bbb"), class = "factor"), `col 3` = c(10.59, 10.56, 10.59,
>>>>> 10.56), `col 4` = c(10.59, 10.56, 10.59, 10.56), `col 5` = c(10.59,
>>>>> 10.56, 10.59, 10.56), `col 6` = structure(c(1L, 2L, 1L, 2L), .Label =
>>>>> c("04-Jul-16",
>>>>> "05-Jul-16"), class = "factor")), .Names = c("col 1", "col 2",
>>>>> "col 3", "col 4", "col 5", "col 6"), class = "data.frame", row.names = c(NA,
>>>>> -4L))
>>>>> Dat
>>>>>
>>>>> write.csv(Dat, "Dat.csv", quote = FALSE, row.names = FALSE)
>>>>>
>>>>> ReadName = '133261'
>>>>> read.csv.sql("Dat.csv", sql = "select * from file where 'col 1' = ReadName")
>>>>>
>>>>> Loading required package: tcltk
>>>>> Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
>>>>> line 1 did not have 7 elements
>>>>>
>>>>> This code generates above Error. Could you please help me with a
>>>>> pointer where something went wrong? My actual CSV file is quite huge
>>>>> so I cant read it as whole. However basic structure of my original
>>>>> file is similar as above "Dat"
>>>>>
>>>>> Thanks for your time.
>>>>>
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list