Reputation: 55
I want to update column 2 so that the the value pairs update to (a,1)(b,1)(c1) and (d,2)(e,2)(f,2) and (g,3)(h,3)(i,3) and so on. How do I loop through? Here is the sample data frame:
data_set <- as.data.frame(matrix(nrow=9))
data_set$column1_set1 <- c("a","b","c","d","e","f","g","h","i")
data_set$column2_set1 <- c(0,0,0,0,0,0,0,0,0)
data_set <- data_set[,-1]
Upvotes: 2
Views: 348
Reputation: 400
With the given data set you can use this to update column 2 in pairs: a,1 etc Paste comma in the set1 and repeat of 1:3 each=3 times!
data_set$column2_set1 =paste0(data_set$column1_set1,",",rep(1:3, each=3))
=== You could have used mutate as well with dplyr :
data_set%>%
mutate("column2_set1" = paste0(column1_set1,",",rep(1:3, each=3)))
output :
column1_set1 column2_set1
1 a a,1
2 b b,1
3 c c,1
4 d d,2
5 e e,2
6 f f,2
7 g g,3
8 h h,3
9 i i,3
Upvotes: 1
Reputation: 554
data_set <- data.frame(column1_set1 = letters[1:9],
column2_set1 = rep(1:3, each=3))
Upvotes: 3