Converting data frame column from character to numeric

If we need only one column to be numeric But, if all the columns needs to changed to numeric, use lapply to loop over the columns and convert to numeric by first converting it to character class as the columns were factor. Both the columns in the OP’s post are factor because of the string “n/a”. This could be easily avoided while reading the file using na.strings = … Read more

Categories R

Remove rows with all or some NAs (missing values) in data.frame

Also check complete.cases : na.omit is nicer for just removing all NA‘s. complete.cases allows partial selection by including only certain columns of the dataframe: Your solution can’t work. If you insist on using is.na, then you have to do something like: but using complete.cases is quite a lot more clear, and faster.

Categories R

Reshaping data.frame from wide to long format

Three alternative solutions: 1) With data.table: You can use the same melt function as in the reshape2 package (which is an extended & improved implementation). melt from data.table has also more parameters that the melt-function from reshape2. You can for example also specify the name of the variable-column: which gives: Some alternative notations: 2) With tidyr: Some alternative notations: 3) With reshape2: Some alternative notations that give the … Read more

Categories R

Increase number of axis ticks

You can override ggplots default scales by modifying scale_x_continuous and/or scale_y_continuous. For example: Gives you this: And overriding the scales can give you something like this: If you want to simply “zoom” in on a specific part of a plot, look at xlim() and ylim() respectively. Good insight can also be found here to understand the other arguments as well.

Generate a set of random unique integers from an interval

sample (or sample.int) does this: will generate ten random numbers from the range 1–100. You probably want replace = TRUE, which samples with replacing: More generally, sample samples n observations from a vector of arbitrary values.

Categories R

Linear model function lm() error: NA/NaN/Inf in foreign function call (arg 1)

Say I have data.frame a I use col2 has some NA values, col3 and col4 have values less than 1. I keep getting I’ve checked the mailing list and it appears that it is because of the NAs in col2 but I tried using na.action=na.exclude/omit/pass but none of them seem to work. I’ve tested lm again on first 10 entries, definitely not because of the NAs. Problem with this warning is … Read more

Categories R

file.path function in R

I am learning function called file.path() in R. I am wondering whether this command will change the working directory just like setwd() or simply give R the path of the file and change the workspace only? Thanks in advance.

Categories R

How do you create vectors with specific intervals in R?

In R the equivalent function is seq and you can use it with the option by: In addition to by you can also have other options such as length.out and along.with. length.out: If you want to get a total of 10 numbers between 0 and 1, for example: along.with: It takes the length of the vector you supply as input and provides a … Read more

Categories R

Convert date-time string to class Date

You may be overcomplicating things, is there any reason you need the stringr package? You can use as.Date and its format argument to specify the input format of your string. Note the Details section of ?as.Date: Character strings are processed as far as necessary for the format specified: any trailing characters are ignored Thus, this also works: All the conversion specifications that can be … Read more

Categories R