R searching for information within column

I have two tables. Which have the kind of formatting shown below. One of it is table A as such: students|Test Score A | 100 B | 81 C | 92 D | 88 Another table B I have looks like this: Class | Students 1 | {A,D}...
more »

2017-04-17 23:04 (5) Answers

Underlay a vector image to a grid for kriging in R

After searching around a lot, asking, and doing some code, I kinda got the bare minimum for doing kriging in R's gstat. Using 4 points (I know, totally bad), I kriged the unsampled points located between them. But in actuality, I don't need all of t...
more »

2017-04-17 08:04 (2) Answers

How to pass column names into a function dplyr

I'm trying to create a simple summary function to speed up the reporting of multiple columns of data for use in a R Markdown file. var1 is a categorical column of data, t_var is an integer representing the quarter of data, and dt is the full data. ...
more »

2017-04-16 15:04 (1) Answers

Calculate sum sequence by sequence

Suppose I have a data.table data.table(A=c(1,2,3,4,5,6,4,2)) How can I calculate the sum of a sequences of n elements? Suppose n=3, The result of the sequence sum of A should be the column seq_sum, data.table(A=c(1,2,3,4,5,6,4,2),seq_sum=c(1+2+...
more »

2017-04-16 09:04 (6) Answers

R: faster merging of data frames

I have just started using R and wrote the following code but this is taking about 40 mins to process, so I am sure this can be coded in a way that it runs faster. Basically, I have one large data set (crsp_td_net) of about 7GB n size and a second s...
more »

2017-04-15 23:04 (3) Answers

No matching %in% when using data.table

Why is it, that the following returns me "TRUE" ifelse(13 %in% 1:13, TRUE, FALSE) [1] TRUE and the following (for me more or less the same) returns "FALSE" TestData <- as.data.table(1:13) ifelse(13 %in% TestData[1:13, "V1"], TRUE, FALSE) [1] F...
more »

2017-04-15 18:04 (2) Answers

Splitting 1 column into 1 to 3 columns in R

I have been wrestling with some code for a personal project and have been hitting some roadblocks. I have some restaurant data and there is a column for the table with information separated by "/". For example : 4/1 means table 4, and first che...
more »

2017-04-15 07:04 (1) Answers

Summarise with first value

I encountered strange behaviors while using summarise. df <- data.frame(id = c(1, 2, 3, 3, 4), color = c(NA, "blue", "red", "blue", NA), stringsAsFactors = FALSE) df # id color # 1 1 <NA> # 2 2 blue #...
more »

2017-04-15 03:04 (0) Answers

Randomly generate 3 distinct colors

I've looked at this one but does not help with the random part. Is there a better way to randomly generate 3 different colors so that the square, the circle, and the text stand out visually from one another in the code below. The colors have to be ra...
more »

2017-04-15 01:04 (2) Answers

Apply the same functions to multiple datasets in R

I am new to R and I have 25 samples of RNAseq results. I would like to apply the same functions to calculate correlation of my target gene (say like gene ABC) to all the 25 samples. I know how to do this individually. Here is my code to do it: df &...
more »

2017-04-14 21:04 (1) Answers

How to keep `[<-` side-effects-free?

I just discovered that `[<-` may have destructive side-effects or not depending on what arguments one gives it. Example x <- 1:5 x # [1] 1 2 3 4 5 `[<-`(x, 1:5, 1) # [1] 1 1 1 1 1 x # [1] 1 2 3 4 5 # ∴ last application of `[<...
more »

2017-04-14 15:04 (0) Answers

Extract date from given string in r

string<-c("Posted 69 months ago (7/4/2011)") library(gsubfn) strapplyc(string, "(.*)", simplify = TRUE) I apply above function but nothing happens. In this I want to extract only date part i.e 7/4/2011. ...
more »

2017-04-14 07:04 (3) Answers

R's equivalent of Python's re.findall

I am trying to get all matches for a RegExp from a string but apparently it's not so easy in R, or I have overlooked something. Truth be told, it's really confusing and I found myself lost among all the options: str_extract, str_match, str_match_all,...
more »

2017-04-13 22:04 (1) Answers

Using the "format" argument in as.Date()

I'm trying to convert "07,29,30" into a Date format using as.Date() in R. (Note that the format of the string is "mm,dd,yy", being July 29, 1930) So I used the following code: as.Date("07,29,30", format = "%m,%d,%y") But the year that I get retu...
more »

2017-04-13 15:04 (4) Answers

rmarkdown pdf font not available

I have a similar question as this one, but I'm getting a different error. I want to set the mainfont in yaml to roboto, but I get the error "fontspec error: "font-not-found"" when I knit it to PDF. --- title: "My Title" header-includes: - \usepac...
more »

2017-04-12 20:04 (2) Answers

What are the dangers of using R attributes?

Adding used-defined attributes to R objects makes it easy to carry around some additional information glued together with the object of interest. The problem is that it slightly changes how R sees the objects, e.g. a numeric vector with additional at...
more »

2017-04-12 14:04 (1) Answers

How can I match emoji with an R regex?

I want to determine which elements of my vector contain emoji: x = c('😂', 'no', '🍹', '😀', 'no', '😛', '䨺', '감사') x # [1] "\U0001f602" "no" "\U0001f379" "\U0001f600" "no" "\U0001f61b" "䨺" "감사" Related ...
more »

2017-04-12 04:04 (1) Answers