Chris
Chris

Reputation: 75

Replace column values for subset of rows using a vector

I am trying to replace the values of a column (cluster) for a subset of rows (value < 6) within a data frame with the values of a vector.

df <- structure(list(value = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), cluster = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("value", "cluster"), row.names = c(NA, 
-10L), class = "data.frame")

new_cluster <- structure(list(cluster = c(1, 1, 2, 2, 2)), .Names = "cluster", row.names = c(NA, 
-5L), class = "data.frame")

I know how to change the column values for a subset of rows to one value:

df[df$value<6,]$cluster <- 1

However I would like to reference a vector (new_cluster) rather than a single value. I've tried the below which doesn't give the desired output however I'm sure there must a simple way to do this.

df[df$value<6,]$cluster <- new_cluster

I'm trying to achieve the below data frame:

     value       cluster
1      1             1
2      2             1
3      3             2
4      4             2
5      5             2
6      6             0
7      7             0
8      8             0
9      9             0
10    10             0

Upvotes: 3

Views: 8963

Answers (1)

Paul Hiemstra
Paul Hiemstra

Reputation: 60934

You need to get the vector out of new_cluster:

df[df$value<6,]$cluster = new_cluster$cluster

The replacement only works if the objects on both sides of the = are the same type, in this case a vector on the LHS and a data.frame with one column in the RHS.

Upvotes: 4

Related Questions