r-faq

R round to nearest .5 or .1

会有一股神秘感。 提交于 2019-12-17 05:01:12
问题 I have a data set of stock prices that have already been rounded to 2 decimal places (1234.56) . I am now trying to round to a specific value which is different for each stock. Here are some examples: Current Stock Price Minimum Tick Increment Desired Output 123.45 .50 123.50 155.03 .10 155.00 138.24 .50 138.00 129.94 .10 129.90 ... ... ... I'm not really sure how to do this but am open to suggestions. 回答1: Probably, round(a/b)*b will do the work. > a <- seq(.1,1,.13) > b <- c(.1,.1,.1,.2,.3,

as.Date returning NA while converting from 'ddmmmyyyy'

↘锁芯ラ 提交于 2019-12-17 04:07:05
问题 I am trying to convert the string "2013-JAN-14" into a Date as follow : sdate1 <- "2013-JAN-14" ddate1 <- as.Date(sdate1,format="%Y-%b-%d") ddate1 but I get : [1] NA What am I doing wrong ? should I install a package for this purpose (I tried installing chron) . 回答1: Works for me. The reasons it doesn't for you probably has to do with your system locale. ?as.Date has the following to say: ## This will give NA(s) in some locales; setting the C locale ## as in the commented lines will overcome

What does %>% function mean in R?

柔情痞子 提交于 2019-12-17 03:46:09
问题 I have seen the use of %>% (percent greater than percent) function in some packages like dplyr and rvest . What does it mean? Is it a way to write closure blocks in R? 回答1: %...% operators %>% has no builtin meaning but the user (or a package) is free to define operators of the form %whatever% in any way they like. For example, this function will return a string consisting of its left argument followed by a comma and space and then it's right argument. "%,%" <- function(x, y) paste0(x, ", ",

Convert UNIX epoch to Date object

雨燕双飞 提交于 2019-12-17 02:42:11
问题 I'm plotting and performing calculations on uniformly distributed time series. The timestamps are currently stored as integers representing the number of seconds since the UNIX epoch (e.g. 1352068320 ), but Date objects seem more appropriate for plotting. How can I do the conversion? I've read ?Date , ?as.Date and ??epoch , but seem to have missed that information. 回答1: Go via POSIXct and you want to set a TZ there -- here you see my (Chicago) default: R> val <- 1352068320 R> as.POSIXct(val,

How does one reorder columns in a data frame?

一笑奈何 提交于 2019-12-17 02:03:33
问题 How would one change this input (with the sequence: time, in, out, files): Time In Out Files 1 2 3 4 2 3 4 5 To this output (with the sequence: time, out, in, files)? Time Out In Files 1 3 2 4 2 4 3 5 Here's the dummy R data: table <- data.frame(Time=c(1,2), In=c(2,3), Out=c(3,4), Files=c(4,5)) table ## Time In Out Files ##1 1 2 3 4 ##2 2 3 4 5 回答1: Your dataframe has four columns like so df[,c(1,2,3,4)] . Note the first comma means keep all the rows, and the 1,2,3,4 refers to the columns. To

How does one reorder columns in a data frame?

泄露秘密 提交于 2019-12-17 02:03:01
问题 How would one change this input (with the sequence: time, in, out, files): Time In Out Files 1 2 3 4 2 3 4 5 To this output (with the sequence: time, out, in, files)? Time Out In Files 1 3 2 4 2 4 3 5 Here's the dummy R data: table <- data.frame(Time=c(1,2), In=c(2,3), Out=c(3,4), Files=c(4,5)) table ## Time In Out Files ##1 1 2 3 4 ##2 2 3 4 5 回答1: Your dataframe has four columns like so df[,c(1,2,3,4)] . Note the first comma means keep all the rows, and the 1,2,3,4 refers to the columns. To

R: Permutations and combinations with/without replacement and for distinct/non-distinct items/multiset

百般思念 提交于 2019-12-17 01:46:50
问题 In this thread, I am trying to include all the commonly asked questions and their answers here. I hope this will be useful for someone. General question : How to generate sequences of r objects from n objects? combination vs permutation. with replacement vs without replacement. distinct items vs non-distinct items (multisets). There are in total 2^3=8 questions of this type. [Update] Josh O'Brien suggests that the 8 questions are related to twelvefold way. Indeed, the "distinct" questions are

R: Permutations and combinations with/without replacement and for distinct/non-distinct items/multiset

浪子不回头ぞ 提交于 2019-12-17 01:45:12
问题 In this thread, I am trying to include all the commonly asked questions and their answers here. I hope this will be useful for someone. General question : How to generate sequences of r objects from n objects? combination vs permutation. with replacement vs without replacement. distinct items vs non-distinct items (multisets). There are in total 2^3=8 questions of this type. [Update] Josh O'Brien suggests that the 8 questions are related to twelvefold way. Indeed, the "distinct" questions are

How to remove all whitespace from a string?

馋奶兔 提交于 2019-12-17 01:39:07
问题 So " xx yy 11 22 33 " will become "xxyy112233" . How can I achieve this? 回答1: In general, we want a solution that is vectorised, so here's a better test example: whitespace <- " \t\n\r\v\f" # space, tab, newline, # carriage return, vertical tab, form feed x <- c( " x y ", # spaces before, after and in between " \u2190 \u2192 ", # contains unicode chars paste0( # varied whitespace whitespace, "x", whitespace, "y", whitespace, collapse = "" ), NA # missing ) ## [1] " x y " ## [2] " ← → " ## [3]

How to save a plot as image on the disk?

痴心易碎 提交于 2019-12-16 20:05:42
问题 I plot a simple linear regression using R. I would like to save that image as PNG or JPEG, is it possible to do it automatically? (via code) There are two different questions: First, I am already looking at the plot on my monitor and I would like to save it as is. Second, I have not yet generated the plot, but I would like to directly save it to disk when I execute my plotting code. 回答1: There are two closely-related questions, and an answer for each. 1. An image will be generated in future