user36476 user36476 - 27 days ago 4
R Question

suppress repeated values in a column in a data.frame in r

I have a simple data.frame

mydf <- read.csv("mydf.csv")
mydf

SOC Preferred.Term Serious Non.Serious
cardiac disorders dizziness 0 0
cardiac disorders dizziness postural 0 1
cardiac disorders palpitations 0 0
cardiac disorders dyspnoea 0 1
cardiac disorders oedema peripheral 0 0
eye disorders lacrimation increased 0 3
eye disorders eye pain 0 0
eye disorders mydriasis 0 0
eye disorders vision blurred 0 0
eye disorders visual impairment 0 1
oral disorders dental caries 0 0
oral disorders tooth fracture 0 0


You can see that the column titled SOC has several repeats.
I want to present this as a table where I only show the 1st instance of any unique term in the SOC column.
In other words, I want the table to look like this:

SOC Preferred.Term Serious Non.Serious
cardiac disorders dizziness 0 0
dizziness postural 0 1
palpitations 0 0
dyspnoea 0 1
oedema peripheral 0 0
eye disorders lacrimation increased 0 3
eye pain 0 0
mydriasis 0 0
vision blurred 0 0
visual impairment 0 1
oral disorders dental caries 0 0
tooth fracture 0 0


Is there a simple/quick way to modify my data.frame to remove the repeats in the 1st column?

Alternatively, is there some type of presentation package in R that will make a table that will eliminate the repeated terms in the 1st column?

Thank you for any suggestions.

Jay Jay
Answer

A simple way that assumes the variable SOC is sorted and is of class character:

mydf$SOC[duplicated(mydf$SOC)] <- ""
Comments