How to compute land cover area in R

Basically, I computed a global distribution probability model in the form of ASCII, say: gdpm. gdpm's values are all between 0 and 1. Then I imported a local map from shape file: shape <- file.choose() map <- readOGR(shape, basename(file_pa...
more »

2017-12-03 10:12 (2) Answers

Rcpp function cannot be found by R

I built an R package (called myUtils), which uses a cpp file, in RStudio following Hadley's manual. My cpp file resides in the src directory, created after running: devtools::use_rcpp(), and under my R directory I have a file called myUtils.R, with t...
more »

2017-12-02 22:12 (1) Answers

Wildcards for filter function in dplyr

I am using dplyr and I would like to filter my dataframe (biotypes) according to sample IDs which are the first column of the data frame, e.g. they look like this: ID chrX.tRNA494-SerAGA chrX.tRNA636-AlaCGC mmu_piR_000007 ... I want to filter IDs...
more »

2017-12-02 16:12 (2) Answers

Multiple values in one cell

I have data looking somewhat similar to this: number type results 1 5 x, y, z 2 6 a 3 8 x 1 5 x, y Basically, I have data in Excel that has commas in a couple of individual cells and I ...
more »

2017-12-02 15:12 (3) Answers

Positive lookbehind on non-ASCII characters in R

I have an R function which tries to capitalise the first letter of every "word" proper = function(x){ gsub("(?<=\\b)([[:alpha:]])", "\\U\\1", x, perl = TRUE) } This works pretty well, but when I have a word with a Māori macron in it like Mā...
more »

2017-12-02 07:12 (2) Answers

Flatten recursive list

There are quite a few questions apparently on this topic, but I can't see any general solution proposed: I have a deeply recursive list and want to flatten it into a single list of all the non-list items contained. For example, take this nested list...
more »

2017-12-02 02:12 (3) Answers

Remove hex values from data.table in R

I have a large data table called Site (300,000 rows, 100 columns). Throughout the data table are hex values, for example: "\x96" or "\xc9." I want all of these values to be removed. They follow the format of "\x" followed by two characters (numbers o...
more »

2017-12-01 20:12 (1) Answers

How to Access the List Indices

I have a list of numbers (index positions really) like so: > original [[1]] [1] 99 100 [[2]] [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 [[3]] [1] 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 ...
more »

2017-12-01 06:12 (2) Answers

Draw a box around a legend ggplot2

I created a plot with a custom legend in ggplot2. I tryed to dray a box around all the items of the legend, however I drawed a box around each item. How can I create only one box around all the items? library(ggplot2) ggplot(mpg, aes(displ, cty)) +...
more »

2017-12-01 01:12 (2) Answers

Order of operations in summarise

Can sombody explain what is happening in the first line of code and why the result differs from the next two lines? library(tidyverse) library(magrittr) data.frame(A=c(2,2),B=c(1,1)) %>% summarise(A = sum(A),B = sum(B), D=sum(A)-sum(B)) yie...
more »

2017-11-30 11:11 (0) Answers

Find common third on large data set

I have a large dataframe like df <- data.frame(group= c("a","a","b","b","b","c"), person = c("Tom","Jerry","Tom","Anna","Sam","Nic"), stringsAsFactors = FALSE) df group person 1 a Tom 2 a Jerry 3 b Tom 4 ...
more »

2017-11-29 17:11 (1) Answers

Apply a function to every nth element in vector

I have a vector in R: > v <- c(5, 10, 15, 20, 25, 30, 35, 40, 45, 50) I would like to apply a function to every nth element of the vector and have it return the new vector. For example, let's say I would like to multiply every third element ...
more »

2017-11-29 11:11 (5) Answers

Set working directory to an IP address

I have set up a NAS storage space on an IP address, for example, now I want to set R working directory to my NAS folder, so I use setwd("//") Then the error shows like this : Error in setwd("//140.114.60...
more »

2017-11-29 10:11 (0) Answers

Identify differences in text paragraphs with R

I would like to use R to compare written text and extract sections which differ between the elements. Consider a and b two text paragraphs. One is a modified version of the other: a <- "This part is the same. This part is old." b <- "This str...
more »

2017-11-29 10:11 (1) Answers

Convert a string to logical in R with sparklyr

I have 100 million rows stored in many .csv files in a distributed file system. I'm using spark_read_csv() to load the data without issue. Many of my columns are stored as character logical values: "true", "false", "<na>". I do not have cont...
more »

2017-11-28 20:11 (1) Answers