C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# Subset data in R

By : user2954086
Date : November 21 2020, 07:31 AM
This might help you Let’s say that we have two variables x1 and x2, and I need to subset the data that x1 = {a,b,c,d} , You can use %in%:
code :
``````> mydata[mydata\$x1 %in% c("a", "b", "c", "d"), ]
x1 x2
1   a  1
2   b  3
3   c  5
5   a  0
6   d  5
7   b  4
9   a  2
10  c  9
12  a  6
13  b  0
14  d  6
15  c  1
18  b  6
``````
``````subset(mydata, subset = x1 %in% c("a", "b", "c", "d"))
``````

Share :

## Subset data.table altering subset limit between factor levels

By : Ryan Pitts
Date : March 29 2020, 07:55 AM
seems to work fine I am having a hard time trying to subset a data.table (package) in R. Giving a following example , How about :
code :
``````DT[,.SD[seq(match(1,cond)+1,.N)],by=ind]
ind cond     dist
[1,]   a    0 3.937058
[2,]   a    1 5.037681
[3,]   a    2 6.036432
[4,]   b    0 3.010903
[5,]   b    2 3.937765
[6,]   b    1 4.976273
``````

## How subset a data frame by a factor and repeat a plot for each subset?

By : Raghav_Core_Develope
Date : March 29 2020, 07:55 AM
Hope this helps Because you want to split up the dataset and make a plot for each level of a factor, I would approach this with one of the split-apply-return tools from the plyr package.
Here is a toy example using the mtcars dataset. I first create the plot and name it p, then use dlply to split the dataset by a factor and return a plot for each level. I'm taking advantage of %+% from ggplot2 to replace the data.frame in a plot.
code :
``````p = ggplot(data = mtcars, aes(x = wt, y = mpg)) +
geom_line()

require(plyr)
dlply(mtcars, .(cyl), function(x) p %+% x)
``````
``````plots = dlply(mtcars, .(cyl), function(x) p %+% x)
plots[1]
``````
``````dlply(mtcars, .(cyl), function(x) p %+% x + facet_wrap(~cyl))
``````
``````pdf()
plots
dev.off()
``````
``````library(dplyr)
plots = mtcars %>%
group_by(cyl) %>%
do(plots = p %+% . + facet_wrap(~cyl))

Source: local data frame [3 x 2]
Groups: <by row>

cyl           plots
1   4 <S3:gg, ggplot>
2   6 <S3:gg, ggplot>
3   8 <S3:gg, ggplot>
``````
``````plots\$plots
``````
``````pdf()
plots\$plots
dev.off()
``````

## Calculation on subset of data without first saving subset as new data.frame

By : wronek
Date : March 29 2020, 07:55 AM
hope this fix your issue Just wrap the steps together and please do not use attach in the future:
code :
``````df.new.SUM\$C <- sqrt( with(df.new.SUM, (V1-VV1)^2/sd(V1)^2 +(V2-VV2)^2/sd(V2)^2 ) )
``````

## Subset a data frame by a function that selects a dataset and a column for subset

By : Mahir Kemal Kara
Date : March 29 2020, 07:55 AM
should help you out When you call a function f with an argument x, R wants to pass an object to the function. Writing f(x) makes R look for an object x in the workspace. In your case, x is not defined. x is hiding in another object called Dataset1. R can't find x there, so it tells you that object option1 not found. The object op1 however is defined in the workspace. Telling R where to look for x is also an option, which is why passing Dataset1\$option1 would work. Similarly, you can pass objects directly, as in the case of a character "option1".
To exemplify:
code :
``````Data <- function( selection, dataset){ # this is your function
Dataset1 <- with( dataset,  dataset [selection == 1,])
Dataset1
}
``````
``````Data(selection= op1, dataset= Dataset2)
cases option1 option2
1     1       1       1
3     3       1       1
4     4       1       0
5     5       1       0
6     6       1       0
``````
``````Data(selection= Dataset1\$option1, dataset= Dataset2)
cases option1 option2
1     1       1       1
3     3       1       1
4     4       1       0
5     5       1       0
6     6       1       0
``````
``````Data <- function(selector, dataset){
Dataset1 <- dataset[dataset[,selector] == 1,]
Dataset1
}
Data(selector="option1", dataset= Dataset2)
cases option1 option2
1     1       1       1
3     3       1       1
4     4       1       0
5     5       1       0
6     6       1       0
``````

## Apply function to subset elements that satisfy a criteria across subset of columns in data.table

``````exampleDT[, lapply(.SD, function(x) mean(x[x<examp_threshold])),.SDcols=target_cols]