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,541
  • 3
  • 35
  • 66
user27976
  • 883
  • 2
  • 15
  • 27
  • 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
  • 78,180
  • 16
  • 134
  • 160
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,025
  • 4
  • 27
  • 36