0

There are a few questions concerning this, but my problem doesn't seem to fit in with the others: I have a column called "Time" that can be either 0 or 1. Based on this value, I'd use the values in another column on the same row.

For instance, I want to display a summary of all words in column Node but only for those columns whose Time == 1. In other words: if Time==1, display value of column Node on the same row.

I tried:

summary(d[d$Time=="1", ]$Node)

However, this also displays words that do not belong to Time==1. These words do have a calue of 0 then.

Example:

Let's say this is d:

Time - Node
 0      Banana
 1      Apple
 1      Banana
 0      Strawberry
 1      Apple
 0      Coconut
 1      Peach

The output should be:

Apple 2
Banana 1
Peach 1

And not

Apple 2
Banana 1
Peach 1
Strawberry 0
Coconut 0
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239

1 Answers1

0

You could use droplevels to remove the levels of the factor variable Node while subsetting

summary(droplevels(d[d$Time=="1", ]$Node))
#  Apple Banana  Peach 
#  2      1      1 

Or

 summary(factor(d[d$Time=='1', ]$Node))
akrun
  • 874,273
  • 37
  • 540
  • 662