lapply

R - Select Elements from list that meet the criteria

偶尔善良 提交于 2019-12-08 03:33:11
问题 I had a tough time selecting elements from a list that meet a function. So documenting the same with a solution. check.digits <- function(x){ grepl('^(\\d+)$' , x) } x = "741 abc pqr street 71 15 41 510741" lx = strsplit(x, split = " ", fixed = TRUE) lapply(lx, check.digits) This does not work - lx[[1]][c(lapply(lx, check.digits))] Use - lx[[1]][sapply(lx, check.digits)] thanks!!! 回答1: Given what you're after, perhaps you should just use gregexpr + regmatches : regmatches(x, gregexpr("\\d+",

R lapply statement with index [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-08 02:58:10
问题 This question already has answers here : Access lapply index names inside FUN (12 answers) Closed 4 years ago . Is there a way I can make an lapply statement also show the index? More specifically, consider the following example: mylist <- list(c(5,4),c(3,2), c(1,3)) myfunction<- function(values){ print("Adding values: ") return(values[1] + values[2]) } lapply(mylist, myfunction) Is there a way I can somehow make it print "Adding Values: 1" "Adding Values: 2" etc.. one element of each in the

“undefined columns selected” - when trying to remove na's from df's in list

左心房为你撑大大i 提交于 2019-12-07 20:58:52
问题 I am trying to replicate the success of this solution: remove columns with NAs from all dataframes in list or Remove columns from dataframe where some of values are NA with a list of dataframes: m1<- structure(list(vPWMETRO = c(1520L, 1520L, 1520L, 1520L, 1520L), vPWPUMA00 = c(500L, 900L, 1000L, 1100L, 1200L), v100 = c(96.1666666666667, 71.4615384615385, 68.6363636363636, 22.5, 64.5), v101 = c(5, 15, NA, NA, NA), v102 = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_)), .Names = c(

How to restore attribute after union n igraphs?

允我心安 提交于 2019-12-07 19:31:46
问题 let's say I have n igraphs objects g1 , g2 ,.., gn . They are undirected and weighted graphs, i.e. new weight's attribute should be added. I'd like to union n graphs into the weighted graph g . It is known from the documentation (see ?graph.union ) if the n graphs have the weight attribute, it is renamed by adding a _1 and _2 (and _3 , etc.) suffix, i.e. weight_1 , weight_2 ,..., weight_n . I have seen the answer and wrote the code for n=3 graphs (see below). Edited: library(igraph) rm(list

How do I speed up this _for_ loop? With data.table + lapply?

一曲冷凌霜 提交于 2019-12-07 15:48:04
问题 This code generates a dataset similar to my own: df <- c(seq(as.Date("2012-01-01"), as.Date("2012-01-10"), "days")) df <- as.data.frame(df) df <- rbind(df, df) id <- c(rep.int(1, 10), rep.int(2, 10)) id <- as.data.frame(id) cnt <- c(1:3, 0, 0, 4, 5:8, 0, 1, 0, 1:7) cnt <- as.data.frame(cnt) df <- cbind(id, df, cnt) names(df) <- c("id", "date", "cnt") df$date[df$date == "2012-01-10"] <- "2012-01-20" I'm trying to find the sum of variable 'cnt' that has occurred within the last 7 days.

How to download multiple closing stock prices only with getSymbols into separate xts files?

自闭症网瘾萝莉.ら 提交于 2019-12-07 15:06:57
问题 How can I use getSymbols from the quantmod package to do the following: Download multiple stock price histories Select only the adjusted closing prices--i.e., suppress open/high/low and vol data Save each price history as a separate xts file, with dates I can implement steps 1 and 2, but I'm having trouble with step 3. StackOverflow has several posts on downloading and merging prices for multiple tickers, but I can't find instructions for downloading and saving in separate files. Here's what

different behaviors when passing empty ellipsis arguments

好久不见. 提交于 2019-12-07 08:29:00
问题 This answer brought up the question of how the ellipsis feature in R handles empty arguments. Apparently an empty argument in ... works sometimes (see lapply version below) but not other times (see sapply version). Here's the example: lst <- list(x=matrix(1)) lapply(lst, "[", 1, ) # $x # [1] 1 sapply(lst, "[", 1, ) # Error in lapply(X = X, FUN = FUN, ...) : # argument is missing, with no default From what I can tell, sapply actually just reuses its ... arguments when calling lapply . So I don

Use lapply on a subset of list elements and return list of same length as original in R

拜拜、爱过 提交于 2019-12-07 07:56:46
问题 I want to apply a regex operation to a subset of list elements (which are character strings) using lapply and return a list of same length as the original. The list elements are long strings (derived from reading in long text files and collapsing paragraphs into a single string). The regex operation is valid only for the subset of list elements/strings. I want the non-subsetted list elements (character strings) to be returned in their original state. The regex operation is str_extract from

R - Select Elements from list that meet the criteria

一世执手 提交于 2019-12-07 02:29:28
I had a tough time selecting elements from a list that meet a function. So documenting the same with a solution. check.digits <- function(x){ grepl('^(\\d+)$' , x) } x = "741 abc pqr street 71 15 41 510741" lx = strsplit(x, split = " ", fixed = TRUE) lapply(lx, check.digits) This does not work - lx[[1]][c(lapply(lx, check.digits))] Use - lx[[1]][sapply(lx, check.digits)] thanks!!! Given what you're after, perhaps you should just use gregexpr + regmatches : regmatches(x, gregexpr("\\d+", x)) # [[1]] # [1] "741" "71" "15" "41" "510741" Or, from "qdapRegex", use rm_number : library(qdapRegex) rm

Arguments in read.delim when used in lapply

孤街浪徒 提交于 2019-12-06 20:42:21
I'm using the following code to read a set of CSV files: csvfiles = list.files(pattern = "*.csv", path = "./Data/", full.names = TRUE) ## Merge into one object myfiles = lapply(csvfiles, read.delim) I would like to pass some arguments to the read.delim function in order to skip a set of initial rows, define delimiter and so on but whenever I try doing this R returns an error, as illustrated below: > myfiles = lapply(csvfiles, read.delim(skip = 2)) Error in read.table(file = file, header = header, sep = sep, quote = quote, : argument "file" is missing, with no default What would the correct