subset

Remove rows from a single-column data frame

落爺英雄遲暮 提交于 2019-11-29 10:58:09
When I try to remove the last row from a single column data frame, I get a vector back instead of a data frame: > df = data.frame(a=1:10) > df a 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 > df[-(length(df[,1])),] [1] 1 2 3 4 5 6 7 8 9 The behavior I'm looking for is what happens when I use this command on a two-column data frame: > df = data.frame(a=1:10,b=11:20) > df a b 1 1 11 2 2 12 3 3 13 4 4 14 5 5 15 6 6 16 7 7 17 8 8 18 9 9 19 10 10 20 > df[-(length(df[,1])),] a b 1 1 11 2 2 12 3 3 13 4 4 14 5 5 15 6 6 16 7 7 17 8 8 18 9 9 19 My code is general, and I don't know a priori whether the data

Generate all “unique” subsets of a set (not a powerset)

南楼画角 提交于 2019-11-29 10:33:12
Let's say we have a Set S which contains a few subsets: - [a,b,c] - [a,b] - [c] - [d,e,f] - [d,f] - [e] Let's also say that S contains six unique elements: a, b, c, d, e and f . How can we find all possible subsets of S that contain each of the unique elements of S exactly once? The result of the function/method should be something like that: [[a,b,c], [d,e,f]]; [[a,b,c], [d,f], [e]]; [[a,b], [c], [d,e,f]]; [[a,b], [c], [d,f], [e]]. Is there any best practice or any standard way to achieve that? I would be grateful for a Pseudo-code, Ruby or Erlang example. It sounds like what you are looking

subset data frame based on percentage

送分小仙女□ 提交于 2019-11-29 10:31:52
问题 i have a data frame that contains a data like this : V1 V2 V3 1 2 0.34 1 3 0.31 1 4 0.12 1 5 0.12 the data frame is bigger but that's an example. i want to take a subset of this data frame that has the lowest 20% of V3. how this can be done ? thanks for help 回答1: The subset() function is handy because (among other benefits) it allows you to avoid having to repeatedly mention the name of the data-frame: subset(dataFrame, V3 <= quantile(V3, 0.2)) 回答2: ss <- subset(dataFrame, subset=(dataFrame

subset data frame in R using loop

℡╲_俬逩灬. 提交于 2019-11-29 10:22:22
问题 I have a data frame that looks like this: ---------- index ID date Amount 2 1001 2010-06-08 0 21 1001 2010-10-08 10 6 1002 2010-08-16 30 5 1002 2010-11-25 20 9 1003 2010-01-01 0 8 1003 2011-03-06 10 12 1004 2012-03-12 10 11 1004 2012-06-21 10 15 1005 2010-01-01 30 13 1005 2010-04-06 20 I want to subset this data so that i have new data frames, one for each ID like this index ID date Amount 2 1001 2010-06-08 0 21 1001 2010-10-08 10 and 6 1002 2010-08-16 30 5 1002 2010-11-25 20 and so on. I

Choose variables based on name (simple regular expression)

风格不统一 提交于 2019-11-29 09:29:09
问题 I would like to incorporate variable names that imply what I should do with them. I imagine a dataframe "survey". library(Rlab) # Needed for rbern() function. survey <- data.frame(cbind( id = seq(1:10), likert_this = sample(seq(1:7),10, replace=T), likert_that = sample(seq(1:7), 10, replace=T), dim_bern_varx = rbern(10, 0.6), disc_1 = sample(letters[1:5],10,replace=T))) Now I would like to do certain things with all variables that contain likert , other things with variables that contain bern

How to check whether the elements of an ArrayList are all contained in another ArrayList

空扰寡人 提交于 2019-11-29 09:06:52
How can I easily check to see whether all the elements in one ArrayList are all elements of another ArrayList? Use Collection.containsAll() : boolean isSubset = listA.containsAll(listB); There is a containsAll method in all collections. 来源: https://stackoverflow.com/questions/808394/how-to-check-whether-the-elements-of-an-arraylist-are-all-contained-in-another-a

R applying a function to a subset of a data frame [duplicate]

北战南征 提交于 2019-11-29 08:56:21
This question already has an answer here: Apply function conditionally 1 answer I looked online extensively and did not see an answer to this particular question (I think). The best way for me to explain myself will be with some code that replicates my problem. I made some temp data: x <- runif(100,1,2) y <- runif(100,2,3) z <- c(rep(1,100)) temp <- cbind(x,y,z) temp[1:25,3] = temp[1:25,3] +2 temp <- as.data.frame(temp) And this is what temp looks like x y z 1 1.512620 2.552271 3 2 1.133614 2.455296 3 3 1.543242 2.490120 3 4 1.047618 2.069474 3 . . . . . . . . 27 1.859012 2.687665 1 28 1

Ordered subsets test

强颜欢笑 提交于 2019-11-29 04:53:31
I want to test if an ordered set is a subset of a bigger ordered set. I used tuples and itertools.combinations : def subset_test(a, b): return a in itertools.combinations(b, len(a)) For instance, >>> subset_test((0, 1, 2), (0, 3, 1, 4, 2)) True >>> subset_test((0, 1, 2), (0, 3, 2, 4, 1)) False It works, but is slow when I test big tuples. You can simply use an iterator to keep track of the position in B >>> A = (0, 1, 2) >>> B = (0, 3, 1, 4, 2) >>> b_iter = iter(B) >>> all(a in b_iter for a in A) True Simple way of doing this >>> a = (0, 1, 2) >>> b = (0, 3, 1, 4, 2) >>> filter(set(a)._

Ruby: Array contained in Array, any order [duplicate]

折月煮酒 提交于 2019-11-29 03:16:39
This question already has an answer here: Check if an array is subset of another array in Ruby 4 answers Suppose I have the following Ruby code: array_1 = ['a', 'b'] array_2 = ['a', 'b', 'c'] some_function(array_1, array_2) # => True some_function(array_2, array_1) # => False some_function(['a', 'b'], ['a', 'd']) # => False some_function(['x', 'y'], array_2) # => False I am pretty much looking for some_function to return True when Parameter 2 contains all of the elements in Parameter 1. def f a,b (a-b).empty? end From a previous post, def f a,b (a-b).empty? end will not work the way you expect

split or subset data into 30 minute intervals

冷暖自知 提交于 2019-11-29 02:38:12
I have a data frame of the following form: Temp Depth Light x time date time.at.depth 104 18.59 -2.7 27 21:38 2012-06-20 4 109 18.59 -2.7 27 22:02 2012-06-20 5 110 18.75 -4.0 27 22:07 2012-06-20 5 113 18.91 -2.7 27 22:21 2012-06-20 4 114 18.91 -4.0 27 22:26 2012-06-20 5 115 18.91 -2.7 27 22:31 2012-06-20 5 117 18.91 -2.7 27 22:40 2012-06-20 4 118 18.75 -4.0 27 22:45 2012-06-20 5 119 18.75 -2.7 27 22:50 2012-06-20 5 121 18.59 -4.0 27 22:59 2012-06-20 4 122 18.75 -2.7 27 23:04 2012-06-20 5 123 18.75 -4.0 27 23:09 2012-06-20 5 126 18.59 -2.7 27 23:23 2012-06-20 5 127 18.59 -2.7 27 23:28 2012-06