r - Unique within a column in data frame -
from dataframe this
df <- read.table(text = "string found count 0-025823 0 1 1-042055 1 1 1-018396 1 2 1-018396 1 2 1-002984 1 3 1-002984 1 3 1-002984 1 3", header = true)
i want following output:
string found count desired output 0-025823 0 1 1 1-042055 1 1 1 1-018396 1 2 1 1-018396 1 2 0 1-002984 1 3 1 1-002984 1 3 0 1-002984 1 3 0
the desired output
column showing unique value starting top bottom. first unique value found tagged 1 , rest (duplicates) 0s.
i have used following formula in excel output in excel:
=if(countif($a$2:a2,a2)>1,0,1) sequenceof columns same above.
i have used loops, aggregate
, within
functions these did not give desired result.
you want mark duplicated values 0:
df <- read.table(text = "string found count 0-025823 0 1 1-042055 1 1 1-018396 1 2 1-018396 1 2 1-002984 1 3 1-002984 1 3 1-002984 1 3", header = true) df$unique <- 1 - duplicated(df$string) # string found count unique #1 0-025823 0 1 1 #2 1-042055 1 1 1 #3 1-018396 1 2 1 #4 1-018396 1 2 0 #5 1-002984 1 3 1 #6 1-002984 1 3 0 #7 1-002984 1 3 0
duplicated
returns logical values , use true
/false
coerced 1
/0
when used in arithmetic.
note should not coerce integers. !duplicated(df$string)
instead.
Comments
Post a Comment