subset

Subset data.table by logical column

旧街凉风 提交于 2019-11-27 14:17:46
I have a data.table with a logical column. Why the name of the logical column can not be used directly for the i argument? See the example. dt <- data.table(x = c(T, T, F, T), y = 1:4) # Works dt[dt$x] dt[!dt$x] # Works dt[x == T] dt[x == F] # Does not work dt[x] dt[!x] mnel From ?data.table Advanced: When i is a single variable name, it is not considered an expression of column names and is instead evaluated in calling scope. So dt[x] will try to evaluate x in the calling scope (in this case the global environment) You can get around this by using ( or { or force dt[(x)] dt[{x}] dt[force(x)]

Set global object in Shiny

纵饮孤独 提交于 2019-11-27 14:09:27
Let's say I have the following server.R file in shiny: shinyServer(function(input, output) { output$plot <- renderPlot({ data2 <- data[data$x == input$z, ] # subsetting large dataframe plot(data2$x, data2$y) }) output$table <- renderTable({ data2 <- data[data$x == input$z, ] # same subset. Oh, boy... summary(data2$x) }) }) What can I do in order to not have to run data2 <- data[data$x == input$z, ] within every render call? If I do the following, I get a "object of type 'closure' is not subsettable" error: shinyServer(function(input, output) { data2 <- reactive(data[data$x == input$z, ])

Difference between subset and filter from dplyr

你离开我真会死。 提交于 2019-11-27 12:45:17
问题 It seems to me that subset and filter (from dplyr) are having the same result. But my question is: is there at some point a potential difference, for ex. speed, data sizes it can handle etc? Are there occasions that it is better to use one or the other? Example: library(dplyr) df1<-subset(airquality, Temp>80 & Month > 5) df2<-filter(airquality, Temp>80 & Month > 5) summary(df1$Ozone) # Min. 1st Qu. Median Mean 3rd Qu. Max. NA's # 9.00 39.00 64.00 64.51 84.00 168.00 14 summary(df2$Ozone) # Min

Using grep to help subset a data frame in R

旧街凉风 提交于 2019-11-27 11:47:58
I am having trouble subsetting my data. I want the data subsetted on column x, where the first 3 characters begin G45. My data frame: x <- c("G448", "G459", "G479", "G406") y <- c(1:4) My.Data <- data.frame (x,y) I have tried: subset (My.Data, x=="G45*") But I am unsure how to use wildcards. I have also tried grep() to find the indicies: grep ("G45*", My.Data$x) but it returns all 4 rows, rather than just those beginning G45, probably also as I am unsure how to use wildcards. It's pretty straightforward using [ to extract: grep will give you the position in which it matched your search pattern

Check if list<t> contains any of another list

非 Y 不嫁゛ 提交于 2019-11-27 11:16:57
I have a list of parameters like this: public class parameter { public string name {get; set;} public string paramtype {get; set;} public string source {get; set;} } IEnumerable<Parameter> parameters; And a array of strings i want to check it against. string[] myStrings = new string[] { "one", "two"}; I want to iterate over the parameter list and check if the source property is equal to any of the myStrings array. I can do this with nested foreach's but i would like to learn how to do it in a nicer way as i have been playing around with linq and like the extension methods on enumerable like

How to plot a subset of a data frame in R?

对着背影说爱祢 提交于 2019-11-27 10:37:26
问题 Is there a simple way to do this in R: plot(var1,var2, for all observations in the data frame where var3 < 155) It is possible by creating a new data newdata <- data[which( data$var3 < 155),] but then I have to redefine all the variables newvar1 <- newdata$var1 etc. 回答1: with(dfr[dfr$var3 < 155,], plot(var1, var2)) should do the trick. Edit regarding multiple conditions: with(dfr[(dfr$var3 < 155) & (dfr$var4 > 27),], plot(var1, var2)) 回答2: Most straightforward option: plot(var1[var3<155],var2

How to detect that a vector is subset of specific vector?

浪子不回头ぞ 提交于 2019-11-27 07:56:55
问题 I have two vectors (sets) like this: first<-c(1,2,3,4,5) second<-c(2,4,5) how can I detect that whether second is subset of first or not? is there any function for this? 回答1: Here's one way > all(second %in% first) [1] TRUE 回答2: Here's another setequal(intersect(first, second), second) ## [1] TRUE Or all(is.element(second, first)) ## [1] TRUE 回答3: If the order of the array elements matters, string conversion could help: ord_match <- function(x,y){ m <- c(0,grep(paste0(x,collapse=""), paste0(y

How to subset a matrix with different column positions for each row? [duplicate]

佐手、 提交于 2019-11-27 07:50:29
问题 This question already has an answer here: Subset a matrix according to a columns vector 2 answers I want to subset a matrix using different (but one) column for every row. So propably apply could do the job? But propably also smart subsetting could work, but i havent found a solution. Computation time is an issue - I have a solution with a for loop, but loading the matrix in the RAM several times is just too slow. Here is an example: Matrix M and vector v are given, M<-matrix(1:15,nrow=5,ncol

How to define the subset operators for a S4 class?

女生的网名这么多〃 提交于 2019-11-27 07:07:20
I am having trouble figuring out the proper way to define the [ , $ , and [[ subset operators for an S4 class. Can anyone provide me with a basic example of defining these three for an S4 class? Discover the generic so that we know what we are aiming for > getGeneric("[") standardGeneric for "[" defined from package "base" function (x, i, j, ..., drop = TRUE) standardGeneric("[", .Primitive("[")) <bytecode: 0x32e25c8> <environment: 0x32d7a50> Methods may be defined for arguments: x, i, j, drop Use showMethods("[") for currently available ones. Define a simple class setClass("A", representation

Update subset of values in a dataframe column

ぐ巨炮叔叔 提交于 2019-11-27 06:30:25
问题 Here's an excerpt of my dataframe: x y se 4 a 7.146329 15 a 8.458633 17 a 9.286849 11 b 6.700024 8 b 4.697962 12 c 7.884244 10 c 7.834816 17 c 7.762385 12 d 5.910785 15 d 12.98158 I need to update the first column, so that each number will be subtracted by 1, but only for conditions a and b. That is, instead of c(4, 15, 17, 11, 8, 12, 10, 17, 12, 15) , I would get c(3, 14, 16, 10, 7, 12, 10, 17, 12, 15) . 回答1: Could use ifelse here. Assuming data frame is named df1 : df1$x <- ifelse(df1$y %in