1

I have a data frame that is based on a weekly food diary for survey participants. The data has been cleaned so that, for each food item, all the Monday intakes are recorded on fiMO, all the Tuesday intakes are recorded against fiTU, etc. The values are all numeric.

What I want to do is to identify - and this could simply be to the R output window - the number of unique values across all seven days for the same food item. For example, if 1 was recorded in mo(nday) and also in we(dnesday), then I should only see 1 once. This is across all 4600 respondents.

I tried

 unique(Testdata[,2:8])

But it gave me the number of unique row as defined by the combination of all seven columns, not the number of unique values within all 7 columns taken as a whole.

Using the fake data below, I wish to know the unique values in the set of seven days. I'm doing this for data cleaning, to ensure there are no unexpected values (e.g. out-of-range), hence why I do not care how many times each value occurs.

 fakedays <- data.frame(mo = c(ceiling(runif(100, min=-1, max=10))),
                        tu = c(ceiling(runif(100, min=-1, max=10))),
                        we = c(ceiling(runif(100, min=-1, max=10))),
                        th = c(ceiling(runif(100, min=-1, max=10))),
                        fr = c(ceiling(runif(100, min=-1, max=10))),
                        sa = c(ceiling(runif(100, min=-1, max=10))),
                        su = c(ceiling(runif(100, min=-1, max=10))))

I have looked at the answers here, here, here, and here but they all relate to unique combinations of values across the columns of interest.

I have 32 food items to clean, each with a week's data, hence why I do not wish to do this work single column at time.

Community
  • 1
  • 1
Michelle
  • 1,281
  • 2
  • 16
  • 31

1 Answers1

2

Are you not simply looking for this:

table(unlist(fakedays))
Colonel Beauvel
  • 30,423
  • 11
  • 47
  • 87
  • Thanks, I haven't encountered that command before. And it's just a simple matter of putting the required column numbers in square brackets after the data frame name. – Michelle Sep 04 '15 at 10:40