[R] Replace a column value on condition

Kuma Raj pollaroid at gmail.com
Mon Mar 23 13:58:59 CET 2015


I want to replace column c3 with values from column c2 whenever values
of column Id are 2. In stata I could use replace c3 = c2 if id ==2.
How could I do that in R?

Thanks


Sample data found below:

> dput(df4)
structure(list(c2 = c(42L, 42L, 47L, 47L, 55L, 55L, 36L, 36L,
61L, 61L), c3 = c(68L, 59L, 68L, 50L, 62L, 50L, 63L, 45L, 65L,
45L), id = c(1, 2, 1, 2, 1, 2, 1, 2, 1, 2)), datalabel = "Written by
R.              ", time.stamp = "23 Mar 2015 13:54", .Names = c("c2",
"c3", "id"), formats = c("%9.0g", "%9.0g", "%9.0g"), types = c(253L,
253L, 255L), val.labels = c("", "", ""), var.labels = c("", "",
""), row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9",
"10"), version = 12L, class = "data.frame")



More information about the R-help mailing list