I have a dataframe that has duplicate IDs with unique values. I need to make the IDs unique.
df <- read.csv("test.csv")
ID: A1, A1, A2, A2, A3, A3, A4, A4
Value: 0.5, 0.9, 1.5, 0.8, 2.2, 2.4, 3.1, 0.5
I need to get this dataframe:
ID: A1_1, A1_2, A2_1, A2_2, A3_1, A3_2, A4_1, A4_2
Value: 0.5, 0.9, 1.5, 0.8, 2.2, 2.4, 3.1, 0.5
I tried the following code which adds a column with repeating alternating _1 and _2 and concatenates to the ID:
unique <- c("_1", "_2")
Unique.col <- matrix(rep(unique, ))
unique_ID <- cbind(df, Unique.col)
unique_ID$ID <- paste(unique_ID$ID, unique_ID$Unique.col)
unique_ID
I get the following dataframe where there is a space between the A1 and _1:
ID: A1 _1, A1 _2, A2 _1, A2 _2, A3 _1, A3 _2, A4 _1, A4 _2
Value: 0.5, 0.9, 1.5, 0.8, 2.2, 2.4, 3.1, 0.5
Is there a better way to do this or a way to get rid of the space?