logo
down
shadow

Subset data in R


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 : facebook icon twitter icon
Subset data.table altering subset limit between factor levels

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?

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

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

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

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


By : Radha
Date : March 29 2020, 07:55 AM
This might help you Consider the data.table, exampleDT, , You need to subset in the j expression, like so:
code :
exampleDT[, lapply(.SD, function(x) mean(x[x<examp_threshold])),.SDcols=target_cols]

#   targetA targetB
#1: 9.224008 6.66624
Related Posts Related Posts :
  • Using German characters (ü, ö, ä, etc.) in text analysis (R)
  • R JAGS: Dimension mismatch
  • R - How to create a function that accepts a code block as parameter?
  • How do I manually set geom_bar fill color in ggplot
  • How can I get my points to connect in a plot and show a trend with NA values in data?
  • Read SPecific lines of a CSV file in R-language
  • ggplot stacked bar plot from 2 separate data frames
  • auto.arima not parallelizing
  • Histogram of binned data frame in R
  • R rewriting stringmanipulations implemented in loop to the R-way
  • get first entries in rows of list?
  • Conditionally removing rows from a matrix in R
  • Using a loop to find P(-1.5<Y<1.5) for a range of sample sizes
  • R-Count and list the maximum count row by row
  • Include Iverson Bracket in R documentation
  • update a data frame and environment in R
  • How to write dynamic cumulative multiple in R
  • format time using as.POSIX in R
  • Change the class of multiple columns
  • Remove period and spaces within column headings nested in a list of data frames
  • R: error message --- package error: "functionName" not resolved from current namespace
  • labels with geom_text ggplot2
  • Passing mongodb ISODate in R
  • Importing "csv" file with multiple-character separator to R?
  • Change row names of a table obtained from a lm regression using xtable function
  • R language iterate over R object
  • How do you delete the header in a dataframe?
  • Re coding in R using complicated statement
  • accumulating functions and closures in R
  • How do you combine two columns into a new column in a dataframe made of two or more different csv files?
  • Twitter authentication fails
  • Summing Values of One Vector Conditional on Values of Another Vector
  • draw cube into 3D scatterplot in RGL
  • lme4 translate formula to code in 3-level model
  • How to draw single axis plot in R
  • Combine geom_tile() and facet_grid/facet_wrap and remove space between tiles (ggplot2)
  • Use snpStats with R version 3.0.1
  • Makefile gives strange error while compiling markdown file into .docx file
  • How to determine whether a points lies in an ellipse
  • Summarize data already grouped in r
  • Is the bigvis package for R not available for R version 3.0.1?
  • Operator overloading in R reference classes
  • How to enable user to switch between ggplot2 and gVis graphs in R Shiny?
  • Is there an easy way to separate categorical vs continuous variables into two dataset in R
  • Correct previous year by id within R
  • Installation of rdyncall package for R
  • ggplot2 plot that evaluates the percentage and mean of a third variable at intersecting points
  • Error Handling with Lapply
  • data.table - split multiple columns
  • How to compute the overall mean for several files in R?
  • R: Graph Plotting: Subscripts in the legend like LaTeX
  • Restructuring data in R
  • Distance of pointsfrom cluster centers after K means clustering
  • R incorrect value of date function
  • Package "Imports" not loading in R development package
  • r - run a user defined function several times by taking column elements as parameters
  • Create input$selection to subset data AND radiobuttons to choose plot type in Shiny
  • Generate crosstabulations from dataframe of categorical variables in survey
  • Restructure output of R summary function
  • New behavior in data.table? .N / something with `by` (calculate proportion)
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org