subset

find all subsets that sum to x - using an initial code

耗尽温柔 提交于 2019-12-08 09:46:07
问题 I am trying to build upon a problem, to solve another similar problem... given below is a code for finding the total number of subsets that sum to a particular value, and I am trying to modify the code so that I can return all subsets that sum to that value (instead of finding the count). Code for finding the total number of suibsets that sum to 'sum': /** * method to return number of sets with a given sum. **/ public static int count = 0; public static void countSubsetSum2(int arr[], int k,

Partition a Set into k Disjoint Subset

爷,独闯天下 提交于 2019-12-08 09:21:28
问题 Give a Set S , partition the set into k disjoint subsets such that the difference of their sums is minimal. say, S = {1,2,3,4,5} and k = 2 , so { {3,4}, {1,2,5} } since their sums {7,8} have minimal difference. For S = {1,2,3}, k = 2 it will be {{1,2},{3}} since difference in sum is 0 . The problem is similar to The Partition Problem from The Algorithm Design Manual . Except Steven Skiena discusses a method to solve it without rearrangement. I was going to try Simulated Annealing. So i

How to create a new variable with values from different variables if another variable equals a set value in R?

◇◆丶佛笑我妖孽 提交于 2019-12-08 09:18:15
问题 This question was migrated from Cross Validated because it can be answered on Stack Overflow. Migrated 4 years ago . I have a complicated question that I will try to simplify by simplifying my dataset. Say I have 5 variables: df$Id <- c(1:12) df$Date <- c(NA,NA,a,a,b,NA,NA,b,c,c,b,a) df$va <- c(1.1, 1.4, 2.5, ...) #12 randoms values df$vb <- c(5.9, 2.3, 4.7, ...) #12 other random values df$vc <- c(3.0, 3.3, 3.7, ...) #12 more random values Then I want to create a new variable that takes the

subset list of xts objects

南楼画角 提交于 2019-12-08 08:28:14
问题 There's a great post on here that has clear examples of subsetting an xts object: Return data subset time frames within another timeframes? However, I have list of xts objects and I am looking to subset each component of this list by gathering all the observations from a specified date onward. So, something like this, but for each xts object in the list: my_xts["2011/"] Thank you for all your help! 回答1: You can use lapply to loop over all the elements in your list, and use an anonymous

R: How to pass a list of selection expressions (strings in this case) to the subset function?

时间秒杀一切 提交于 2019-12-08 08:26:52
问题 Here is some example data: data = data.frame(series = c("1a", "1b", "1e"), reading = c(0.1, 0.4, 0.6)) > data series reading 1 1a 0.1 2 1b 0.4 3 1e 0.6 Which I can pull out selective single rows using subset: > subset (data, series == "1a") series reading 1 1a 0.1 And pull out multiple rows using a logical OR > subset (data, series == "1a" | series == "1e") series reading 1 1a 0.1 3 1e 0.6 But if I have a long list of series expressions, this gets really annoying to input, so I'd prefer to

Filtering a Dataset by another Dataset in R

删除回忆录丶 提交于 2019-12-08 07:23:07
问题 The task I am trying to accomplish is essentially filtering one dataset by the entries in another dataset by entries in an "id" column. The data sets I am working with are quite large having 10 of thousands of entries and 30 or so variables. I have made toy datasets to help explain what I want to do. The first dataset contains a list of entries and each entry has their own unique accession number(this is the id). Data1 = data.frame(accession_number = c('a','b','c','d','e','f'), values =c('1',

Select groups with more than one distinct value per group [duplicate]

≯℡__Kan透↙ 提交于 2019-12-08 07:01:06
问题 This question already has answers here : Select groups with more than one distinct value (3 answers) Closed 4 years ago . I have data like below: ID category class 1 a m 1 a s 1 b s 2 a m 3 b s 4 c s 5 d s I want to subset the data by only including those "ID" which have several ( > 1 ) different categories. My expected output: ID category class 1 a m 1 a s 1 b s Is there a way to doing so? I tried library(dplyr) df %>% group_by(ID) %>% filter(n_distinct(category, class) > 1) But it gave me

conditionally subset an additional variable and append it to the previous one in R

◇◆丶佛笑我妖孽 提交于 2019-12-08 04:43:21
问题 I'm following up on this excellent answer . I have a function that subset s what (i.e., a variable) user requests out of this dataset. I was wondering how to add control == TRUE entries IF THEY ARE ABSENT in the output and append those to what the user has requested, otherwise don't do anything. As an example of control == T absent, suppose user wants to subset entries with type == 4 . In this dataset, there are some such entries. As reproducible code and data below show, this is done easily

A replacement for `subset()` for a list of data.frames

眉间皱痕 提交于 2019-12-08 04:35:19
问题 Function foo1 can subset (using subset() ) a list of data.frames by one or more requested variables (e.g., by = ESL == 1 or by == ESL == 1 & type == 4 ). However, I'm aware of the danger of using subset() in R. Thus, I wonder in foo1 below, what I can use instead of subset() to get the same output? foo1 <- function(data, by){ s <- substitute(by) L <- split(data, data$study.name) ; L[[1]] <- NULL lapply(L, function(x) do.call("subset", list(x, s))) ## What to use instead of `subset` ## to get

Passing on non-standard evaluation arguments to the subset function

心已入冬 提交于 2019-12-08 04:11:20
问题 I want to use subset within another function but to pass on the non-standard evaluation arguments from the top-level function. The following is non-working code, but outlines the idea: foo_1 <- function(x, mysubset) { # some deparse, substitute etc. magic here ?? subset(x, subset) } foo_1(ansombe, x1 > 5) I want this to get the same results as for subset(ansombe, x1 > 5) . Also, I want the same to work when the argument is passed on to a deeper level, i.e. foo_2 <- function(x, mysubset) { #