So,I am a very beginner in R,I wanted to ask this-
inside read.csv ,we are using "",but while writting summary/mean/sd etc. we don't use "". Why is this the case?
Double quotes " demark strings of characters, text inside code. Code itself is not double-quoted.
The following is valid code
name.of.file <- "C:\\Users\\bernhard\\valued_data.csv"
read.csv(name.of.file)
So you see, there is nothing special about read.csv, it takes the file name either as a string in "s or as a variable containing that string; no " in the latter case.
Related
I tried with gsub, and str_replace, but I didn't get what I need.
gsub("\"", "\`", "map-09", fixed=TRUE)
gives "map-09" and not map-09
str_replace("map-09","\"","\`")
gives "map-09" and not map-09
I think you're getting confused between the way the string is being represented vs. what the string actually is. The functions you're using don't actually add any quotes. But when the output is printed quotes are there to show you that it is a string being represented. If you use the function 'cat' to print the output you'll see it without the quotes.
See the difference between
print("example")
cat("example")
I have a .csv file similar to this one:
id,text,value
1,'foo, bar',5
2,'foo',5
3,'foo"bar',4
It uses single quotes for text.
I would like to read it in with fread(). I tried fread('text.csv', quote = "\'"), but I get 'unused argument' error. Is it possible to read the file in somehow? Can I somehow change the default quoting character?
(Using command line tool as sed to change the single quotes to double quotes also does not work as some field contains double quotes as well.)
I would like to know how to accommodate more than two types of quotes in a same row in R. Let´s say that I want to print:
'first-quote-type1 "first-quote-type2 "second-quote-type2
'sencond-quote-type1
Using one quote in the beginning and one in the end we have:
print("'first-quote-type1 "first-quote-type2 "second-quote-type2 'sencond-quote-type1")
Error: unexpected symbol in "print("'first-quote-type1 "first"
I tried to include triple quotes as required in Python in this cases:
print(''''first-quote-type1 "first-quote-type2 "second-quote-type2 'sencond-quote-type1''')
print("""'first-quote-type1 "first-quote-type2 "second-quote-type2 'sencond-quote-type1""")
However, I also got a similar error. Some idea how to make this syntax work in R?
To use a quote within a quote you can escape the quote character with a backslash
print("the man said \"hello\"")
However, the print function in R will always escape character.
To not show the escaped character use cat() instead
so...
cat("the man said \"hello\"") will return
the man said "hello"
I'm trying to create a data file (data.file).
I have a string: string=c("test1","test2","test3") which I need to combine with some other commands and write to a text file. string can be of any length.
In this specific case, the output I want in my data file is:
: "test1" "test2" "test3" :=
I tried cat(':',string,':=\n',file=data.file,append=TRUE) but this isn't returning the quotation marks that I need.
(It returns : test1 test2 test3 := which is missing the quotation marks) .
How could I do this? And, can it also be done with writeLines?
writeLines literally just writes lines separated by line separators, nothing more. Formatting has to be done before that.
In order to surround your text by quotes, you can simply do the following:
sprintf('"%s"', string)
sprintf is a versatile text formatting function (although its usage is quite arcane).
However, there’s a problem if your strings can contain quotation marks. What should happen in such a case? In general, one has to pay close attention to what values are allowed for string content before formatting it, and how to deal with unexpected input.
When I tried to read a csv file using data.table:fread(fn, sep='\t', header=T), it gives an "Unbalanced " observed on this line" error. The data has 3 integer variables and 1 string variable. The strings in the csv file are not enclosed with ", and yes there are some lines that contains " within the string variable and the " characters are not in pairs.
I am wondering is it possible to let fread just ignore the unpaired " in the variable and continue reading data? Thanks.
Here is the sample data(just one record)
N_ID VISIT_DATE REQ_URL REQType
175931 2013-3-8 23:40:30 http://aaa.com/rest/api2.do?api=getSetMobileSession&data={"imei":"60893ZTE-CN13cd","appkey":"android_client","content":"Z0JiRA0qPFtWM3BYVltmcx5MWF9ZS0YLdW1ydXoqPycuJS8idXdlY3R0TGBtU 1
UPDATE: Now implemented in v1.8.11
From NEWS :
fread now accepts quotes (both ' and ") in the middle of fields,
whether the field starts with " or not, rather than the 'unbalanced
quotes' error, #2694. Thanks to baidao for reporting. It was known and
documented at the top of ?fread (text now removed). If a field starts
with " it must end with " (necessary if the field separator itself is in the
field contents). Embedded quotes can be in column names too. Newlines (\n)
still can't be in quoted fields or quoted column names, yet.
Yes as #agstudy said, embedded quotes are a known documented problem not yet implemented since fread is new. Strictly speaking, I suppose these ones aren't embedded because the string in your example doesn't start with a quote, though.
Anyway, I've filed this as a bug report so it doesn't get forgotten. To be done in the next release. Thanks for highlighting.
#2694 : Strings including quotes but not starting with quote in fread