1
$\begingroup$

the below dataset is just top 8 rows of 100 rows dataset.

>a
  group marks    upd
2     C     3     up
3     C     4   down
6     C     7     up
1     T     2     up
4     T     5     up
5     T     6   down
7     T     1 middle
8     T     0   down

I used unique_groups=unique(a$group) to get a list of unique group names to be sent into a function.

Now, I need a function that gives a subset of this dataset where it takes each group from unique_groups and checks if 'upd' is in ('down','middle') and gives me a dataset having only those records having upd ('down','middle') of that respective group.

I tried to use do.call function to get the dataset that I want as-

do.call(rbind,sapply(unique_groups,FUN=function(y) { h=a[a$group==x & a$upd %in% c('down','middle'),] h } , simplify=F))

I wasnt getting the dataset that I want and it was giving this warning message:

In ==.default(a$group, x) : longer object length is not a multiple of shorter object length

$\endgroup$
1
  • $\begingroup$ library(dplyr); new.a <- a %>% filter(upd %in% c('down','middle')) $\endgroup$
    – Omri374
    May 11 '16 at 12:31
2
$\begingroup$

This question seemed to be more suitable for general stackoverflow since it's a R question.

I'm using data.table. The syntax is a little bit different from data.frame, but the time spent on learning data.table is well worth the effort. See more tutorials on wiki, and this good sheet.

library(data.table)
s = "rowname    group   marks   upd
2   C   3   up
3   C   4   down
6   C   7   up
1   T   2   up
4   T   5   up
5   T   6   down
7   T   1   middle
8   T   0   down"
# fread is the fastest csv reader
a = fread(s) 
# the i in data.table[i,j] subset rows, empty j select all columns
upd_subset = a[upd %in% c('down','middle'),]

> upd_subset
   rowname group marks    upd
1:       3     C     4   down
2:       5     T     6   down
3:       7     T     1 middle
4:       8     T     0   down

> upd_subset_bygroups = split(upd_subset, upd_subset$group)
> upd_subset_bygroups
$C
   rowname group marks  upd
1:       3     C     4 down

$T
   rowname group marks    upd
1:       5     T     6   down
2:       7     T     1 middle
3:       8     T     0   down
$\endgroup$
0
$\begingroup$

Try this

new_dataframe<- subset(a,upd=="down" | upd=="middle")

$\endgroup$
3
  • $\begingroup$ I need dataset that contains only those groups having upd as 'down' and 'middle'. and also i need a function because I have to send each group into the function and check if its upd contains 'down' and 'middle' and then should give an output as dataframe having only those groups. Your code would also give groups having just 'down' or 'middle'. $\endgroup$
    – pinky
    May 3 '16 at 20:07
  • $\begingroup$ How can I migrate this question to Stack Overflow? $\endgroup$
    – pinky
    May 4 '16 at 17:18
  • $\begingroup$ This response answers the question. You may have to add a column specifier for results. e.g. subset(a,upd=="down", c("group","marks","upd")). The subset function is intended for interactive use. For a script, better to use the format: new_dataframe <- a[a$upd %in% ("middle","down"),c("group","marks","upd")] $\endgroup$ May 6 '16 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.