Data Conversion R - r

How can I convert c(mpg,cyl,disp,hp,drat,wt) to c("mpg","cyl","disp","hp","drat","wt")?
I need to add extra double quotes to each element in the vector

Try either
paste(quote(c(mpg,cyl,disp,hp,drat,wt)))[-1]
Or
as.character(substitute(c(mpg,cyl,disp,hp,drat,wt)))[-1]

Related

toString of HH:MM in R

Having a Dataframe with "15:15" on dataframe[14,3], when I do a toString it prints:
911
What should be the problem here? If I print dataframe[14,3], it correctly prints 15:15.
I am trying to paste three variables and one of them, being in this format, it is appearing as a whole number (which I do not understand the relation with the original string).
The problem would be based on the class of the column. If it is a factor class, then by doing the toString, could change the class to integer storage mode. The option would be to convert it to character and then apply the function
dataframe[,3] <- as.character(dataframe[,3])

Losing information when converting from character to numerical

I'm trying to convert characters like "9.230" to a numeric type.
First I erased the dots, because it was returning me "NA", and then I converted to numerical.
The problem is that when I convert to numerical I lose the trailing zero:
Example:
a<-9.230
as.numeric(gsub(".","",a,fixed=TRUE))
Returns: 923
Does anyone know how avoid this?
You assign the number 9.230 which is the same as 9.23. How is the system supposed to know that there was a trailing zero? If you want to transform a string, work with the string "9.230".
Look for result of
a<-9.230
gsub(".","",a,fixed=TRUE)
#[1] "923"
Question will be why? Because fixed=TRUE have been used in argument of gsub. Hence . is replaced by the 2nd argument of gsub that is "".
Basically thats the reason why as.numeric(gsub(".","",a,fixed=TRUE)) is resulting in 923
There is another point. How a <- 9.230 was changed to character in gsub function. This has been explained in r documentation for gsub:
Arguments: x, text
a character vector where matches are sought, or an object
which can be coerced by as.character to a character vector. Long
vectors are supported.
Final question: How to avoid such behavior?
Dont use gsub. Use sprintf("%.3f",a)

How to convert special characters into unicode in R?

When doing some textual data cleaning in R, I can found some special characters. In order to get rid of them, I have to know their unicodes, for example € is \u20AC. I would like to know if it is possible "see" the unicodes with a function that take into account the string within the special character as an input?
Refering to Cath comment, iconv can do the job :
iconv("é", toRaw = TRUE)
Then, you may want to unlist and paste with \u00.
special_char <- "%"
Unicode::as.u_char(utf8ToInt(special_char))

Put quotes around individual elements in a vector

I have a vector of strings of length in hundreds. Unfortunately, I forgot to put quotes around each element while creating it manually. Is it possible to put these quotes by using a single command instead of doing it manually in R/RStudio?
Example string:
c(122.3.Car, 125.1.Car, 132.3.Car, 140.2.Car, 163.2.Car)
Expected output:
c("122.3.Car", "125.1.Car", "132.3.Car", "140.2.Car", "163.2.Car")

Formatting a number in gridview

i have a gridview that shows some indicators.one of the numbers is a computed percentage it looks like this "33.33333333333333333333333333333333333333" i tried to set the DataFormatString property of the column to several different formats such as "{0:P}" ,"{0:D}" and "{0:##.##}" but nothing worked for me to show it like that "33.34". Any ideas?
What happens if you use "{0:F2}"?
Try entering just the format string without braces and the index,eg. P, D or ##.### . The braces and index are used by String.Format, where there are more than one format parameters. ToString methods (e.g. int.ToString) and control format parameters use only the format string without braces

Resources