By Matthew Leitao


2018-07-12 00:15:05 8 Comments

My question is in attempting to create a new variable based on existing variables.

I have a data set on bunch of different types of classes and class numbers, and whether or not the class was online or offline.

What I am having trouble with is figuring out how to write code to check if there is multiple of the same class that are also different types (i.e. offline or online).

So presumable it would check like this

1. Test Class ,  1, Online
2. Test Class ,  2, Offline
3. Basic Class,  1, Online
4. Test Class ,  3, Offline
5. Basic Class,  2, Offline
6. Test Class ,  1, Offline

so it would report back when two classes match on Class name, class number, and if there is both an online and offline section. Like this

True, False, False, False, False, True

That way I can sort based on the new variable created and compare only similar classes of different section types.

1 comments

@G. Grothendieck 2018-07-12 00:26:12

Using DF from the Note at the end this uses only base R

differ <- function(x) length(x) == 2 && x[1] != x[2]
transform(DF, V4 = ave(as.character(V3), V1, V2, FUN = differ))

giving:

           V1 V2      V3    V4
1  Test Class  1  Online  TRUE
2  Test Class  2 Offline FALSE
3 Basic Class  1  Online FALSE
4  Test Class  3 Offline FALSE
5 Basic Class  2 Offline FALSE
6  Test Class  1 Offline  TRUE

Note

Lines <- "
Test Class ,  1, Online
Test Class ,  2, Offline
Basic Class,  1, Online
Test Class ,  3, Offline
Basic Class,  2, Offline
Test Class ,  1, Offline"

DF <- read.table(text = Lines, sep = ",", strip.white = TRUE)

Related Questions

Sponsored Content

29 Answered Questions

31 Answered Questions

[SOLVED] How to check if a variable is set in Bash?

  • 2010-08-30 14:54:38
  • prosseek
  • 890938 View
  • 1058 Score
  • 31 Answer
  • Tags:   bash shell variables

26 Answered Questions

13 Answered Questions

[SOLVED] How to check a not-defined variable in JavaScript

24 Answered Questions

[SOLVED] How to determine if variable is 'undefined' or 'null'?

16 Answered Questions

[SOLVED] How to sort a dataframe by multiple column(s)?

25 Answered Questions

[SOLVED] What is the scope of variables in JavaScript?

3 Answered Questions

[SOLVED] data.table vs dplyr: can one do something well the other can't or does poorly?

  • 2014-01-29 15:21:45
  • BrodieG
  • 79656 View
  • 563 Score
  • 3 Answer
  • Tags:   r data.table dplyr

9 Answered Questions

[SOLVED] if else statement in AngularJS templates

23 Answered Questions

[SOLVED] How do you check if a variable is an array in JavaScript?

Sponsored Content