2

How can I replace, for example, all "-sh2", in a column (V2) with -100, as in the following dataframe:

V1  V2  V3
p1  -sh2    13
p2  23  29
p3  17  25
p4  -sh2    34

Thanks

FXQuantTrader
  • 6,821
  • 3
  • 36
  • 67
user27976
  • 903
  • 3
  • 17
  • 28
  • Start by reading this [**basic introductory text on vectors and assignment**](http://cran.r-project.org/doc/manuals/R-intro.html#Index-vectors) – Simon O'Hanlon Jan 09 '14 at 14:39

2 Answers2

4
dat$V2 <- replace(as.character(dat$V2), dat$V2 == "-sh2", "-100")
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
2

There are many ways to do this. You can use the replace solution above. Or use ifelse. Or even:

my.df$V2[my.df$V2 == "-sh2"] <- -100
rrs
  • 9,615
  • 4
  • 28
  • 38