How to assign a unique ID number to each group of identical values in a column [duplicate]

How about df2 <- transform(df,id=as.numeric(factor(sample))) ? I think this (cribbed from Add ID column by group) should be slightly more efficient, although perhaps a little harder to remember: df3 <- transform(df, id=match(sample, unique(sample))) all.equal(df2,df3) ## TRUE If you want to do this in tidyverse: library(dplyr) df %>% group_by(sample) %>% mutate(id=cur_group_id())

Combine two arrays

Just use: $output = array_merge($array1, $array2); That should solve it. Because you use string keys if one key occurs more than one time (like ’44’ in your example) one key will overwrite preceding ones with the same name. Because in your case they both have the same value anyway it doesn’t matter and it will … Read more

Add ID column by group [duplicate]

Here’s one way using interaction. d <- read.table(text=”LAT LONG 13.5330 -15.4180 13.5330 -15.4180 13.5330 -15.4180 13.5330 -15.4180 13.5330 -15.4170 13.5330 -15.4170 13.5330 -15.4170 13.5340 -14.9350 13.5340 -14.9350 13.5340 -15.9170 13.3670 -14.6190″, header=TRUE) d <- transform(d, Cluster_ID = as.numeric(interaction(LAT, LONG, drop=TRUE))) # LAT LONG Cluster_ID # 1 13.533 -15.418 2 # 2 13.533 -15.418 2 # … Read more

Filter/Remove rows where column value is found more than once in a multidimensional array

For a clearer “minimal, complete, verifiable example”, I’ll use the following input array in my demos: $array = [ [‘user_id’ => 82, ‘ac_type’ => 1], [‘user_id’ => 80, ‘ac_type’ => 5], [‘user_id’ => 76, ‘ac_type’ => 1], [‘user_id’ => 82, ‘ac_type’ => 2], [‘user_id’ => 80, ‘ac_type’ => 5] ]; // elements [0] and [3] … Read more

Remove duplicate values from JS array [duplicate]

TL;DR Using the Set constructor and the spread syntax: uniq = […new Set(array)]; “Smart” but naïve way uniqueArray = a.filter(function(item, pos) { return a.indexOf(item) == pos; }) Basically, we iterate over the array and, for each element, check if the first position of this element in the array is equal to the current position. Obviously, … Read more

Get all unique values in a JavaScript array (remove duplicates)

With JavaScript 1.6 / ECMAScript 5 you can use the native filter method of an Array in the following way to get an array with unique values: function onlyUnique(value, index, self) { return self.indexOf(value) === index; } // usage example: var a = [‘a’, 1, ‘a’, 2, ‘1’]; var unique = a.filter(onlyUnique); console.log(unique); // [‘a’, … Read more

Count unique values per groups with Pandas [duplicate]

You need nunique: df = df.groupby(‘domain’)[‘ID’].nunique() print (df) domain ‘facebook.com’ 1 ‘google.com’ 1 ‘twitter.com’ 2 ‘vk.com’ 3 Name: ID, dtype: int64 If you need to strip ‘ characters: df = df.ID.groupby([df.domain.str.strip(“‘”)]).nunique() print (df) domain facebook.com 1 google.com 1 twitter.com 2 vk.com 3 Name: ID, dtype: int64 Or as Jon Clements commented: df.groupby(df.domain.str.strip(“‘”))[‘ID’].nunique() You can retain … Read more

vba: get unique values from array

This post contains 2 examples. I like the 2nd one: Sub unique() Dim arr As New Collection, a Dim aFirstArray() As Variant Dim i As Long aFirstArray() = Array(“Banana”, “Apple”, “Orange”, “Tomato”, “Apple”, _ “Lemon”, “Lime”, “Lime”, “Apple”) On Error Resume Next For Each a In aFirstArray arr.Add a, a Next On Error Goto 0 … Read more