简体   繁体   中英

adding characters to duplicated rows in a column

I have a data frame which looks something like this:

          var1         var2
1  927720_2005  927720_2006
2  927720_2006  927720_2007
3  841555_2005  841555_2006
4   88095_2005   88095_2006
5 1003464_2005 1003464_2006
6 1003464_2005 1003464_2006
7 1003464_2006 1003464_2007
8 1037388_2005 1037388_2006
9 1037388_2006 1037388_2007

The observation 1003464_2005 in column var1 is duplicated so when I apply rownames(MyMatrix) <- df$var1 the rownames has one observation as 1003464_2005 and another as 1003464_2005.1 . I don't mind this but the column names are allowed to have duplicates when i use colnames(MyMatrix) <- df$var2 .

I want to make the data such that:

          var1          var2
1  927720_2005   927720_2006
2  927720_2006   927720_2007
3  841555_2005   841555_2006
4   88095_2005   88095_2006
5 1003464_2005   1003464_2006
6 1003464_2005.1 1003464_2006.1
7 1003464_2006   1003464_2007
8 1037388_2005   1037388_2006
9 1037388_2006   1037388_2007

And if I have "3 duplicates" in var1 just add another "counter" on 1003464_2005.2 or 1003464_2005.1.1 . This way I will have no duplicates in the var1 column and what ever has been "added" to the var1 column add to the var2 column.

Thanks in advance for any help!

Data:

df <- structure(list(var1 = structure(c(7L, 8L, 5L, 6L, 1L, 1L, 2L, 
3L, 4L), .Label = c("1003464_2005", "1003464_2006", "1037388_2005", 
"1037388_2006", "841555_2005", "88095_2005", "927720_2005", "927720_2006"
), class = "factor"), var2 = c("927720_2006", "927720_2007", 
"841555_2006", "88095_2006", "1003464_2006", "1003464_2006", 
"1003464_2007", "1037388_2006", "1037388_2007")), class = "data.frame", row.names = c(NA, 
-9L))

With duplicate rownames my matrix looks like the following:

structure(c(0.0000000000000000111365086910415, 0.0242390433922595, 
0.294121286748089, 0.302965878225595, 0.259626633772708, 0.25760904856241, 
0.248574305825551, 0.17848782814175, 0.191657814393258, 0.0242390433922595, 
0.0000000000000000113968217215608, 0.310381807852827, 0.293653514681392, 
0.245957439956465, 0.249142123526167, 0.251115609138352, 0.166302748882678, 
0.176256028117321, 0.294121286748089, 0.310381807852827, -0.00000000000000000151197688178523, 
0.355703128500295, 0.319662657194485, 0.317127296846476, 0.305644319511071, 
0.255031411391534, 0.275597914790561, 0.302965878225595, 0.293653514681392, 
0.355703128500295, 0.00000000000000000801369440490437, 0.309841957462355, 
0.311910981514099, 0.317253692884325, 0.254335300246398, 0.265496031285385, 
0.259626633772708, 0.245957439956465, 0.319662657194485, 0.309841957462355, 
0.0000000000000000105380873106143, 0.0104634838149491, 0.0245937753301301, 
0.221744045353809, 0.22476375867925, 0.25760904856241, 0.249142123526167, 
0.317127296846476, 0.311910981514099, 0.0104634838149491, 0.00000000000000000986038424517971, 
0.0257337720292454, 0.220483645448676, 0.224712591289328, 0.248574305825551, 
0.251115609138352, 0.305644319511071, 0.317253692884325, 0.0245937753301301, 
0.0257337720292454, 0.0000000000000000121630774340264, 0.213285559165696, 
0.229922308724439, 0.17848782814175, 0.166302748882678, 0.255031411391534, 
0.254335300246398, 0.221744045353809, 0.220483645448676, 0.213285559165696, 
0.0000000000000000139766402734024, 0.0152113168185518, 0.191657814393258, 
0.176256028117321, 0.275597914790561, 0.265496031285385, 0.22476375867925, 
0.224712591289328, 0.229922308724439, 0.0152113168185518, 0.0000000000000000120926010568502
), .Dim = c(9L, 9L), .Dimnames = list(c("927720_2005", "927720_2006", 
"841555_2005", "88095_2005", "1003464_2005", "1003464_2005", 
"1003464_2006", "1037388_2005", "1037388_2006"), c("927720_2005", 
"927720_2006", "841555_2005", "88095_2005", "1003464_2005", "1003464_2005", 
"1003464_2006", "1037388_2005", "1037388_2006")))

It can be easily done with make.unique . Loop through the columns of interest, and then apply the function make.unique . It expects the column to be character class. According to ?make.unique

names - a character vector

So, if it is factor , then convert it to character

df[] <- lapply(df, function(x) make.unique(as.character(x)))

In dplyr , this can be done similarly, but with mutate_if

library(dplyr)
df %>%
   mutate_if(is.factor, as.character) %>%
   mutate_if(is.character, make.unique)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM