[R] How to replace numeric value in the column contains Text (Factor)?

Marna Wagley m@rn@@w@gley @ending from gm@il@com
Wed Apr 18 19:55:40 CEST 2018


Hi R user,
Would you mind to help me on how I can change a value in a specific column
and row in a big table? but the column of the table is a factor (not
numeric).
Here is an example. I want to change dat[4:5,3]<-"20" but it generated NA>
do you have any suggestions for me?

dat<-structure(list(Sites = structure(1:5, .Label = c("Site1", "Site2",
"Site3", "Site4", "Site5"), class = "factor"), temp = c(14, 15,
12, 12.5, 17), precip = structure(c(3L, 4L, 5L, 2L, 1L), .Label = c("15",
"34", "high", "low", "medium"), class = "factor")), .Names = c("Sites",
"temp", "precip"), class = "data.frame", row.names = c(NA, -5L
))
> dat[4:5, 3] <-"20"
Warning message:
In `[<-.factor`(`*tmp*`, iseq, value = c("20", "20")) :
  invalid factor level, NA generated
Thanks,

	[[alternative HTML version deleted]]




More information about the R-help mailing list