Marna Wagley
2018-Apr-18 17:55 UTC
[R] How to replace numeric value in the column contains Text (Factor)?
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]]
Jeff Newmiller
2018-Apr-18 18:12 UTC
[R] How to replace numeric value in the column contains Text (Factor)?
I would recommend that you avoid converting (or letting R convert for you) your textual data values into factors until you have finished doing this kind of modification. You can restore the column to character data type either re-reading it with the stringsAsFactors=FALSE option to read.table/read.csv functions, or by converting it: dat[[ 3 ]] <- as.character( dat[[ 3 ]] ) and then your replacement will work. On April 18, 2018 12:55:40 PM CDT, Marna Wagley <marna.wagley at gmail.com> wrote:>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]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.-- Sent from my phone. Please excuse my brevity.
David L Carlson
2018-Apr-18 18:13 UTC
[R] How to replace numeric value in the column contains Text (Factor)?
The simplest would be to convert precip to character and then back to a factor
if you really want it to be a factor. This will also remove the levels that no
longer exist.
str(dat)
# 'data.frame': 5 obs. of 3 variables:
# $ Sites : Factor w/ 5 levels "Site1","Site2",..: 1 2 3 4
5
# $ temp : num 14 15 12 12.5 17
# $ precip: Factor w/ 5 levels
"15","34","high",..: 3 4 5 2 1
dat$precip <- as.character(dat$precip)
dat[4:5, 3] <-"20"
dat$precip <- factor(dat$precip)
str(dat)
# 'data.frame': 5 obs. of 3 variables:
# $ Sites : Factor w/ 5 levels "Site1","Site2",..: 1 2 3 4
5
# $ temp : num 14 15 12 12.5 17
# $ precip: Factor w/ 4 levels
"20","high","low",..: 2 3 4 1 1
----------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77843-4352
-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Marna Wagley
Sent: Wednesday, April 18, 2018 12:56 PM
To: r-help mailing list <r-help at r-project.org>
Subject: [R] How to replace numeric value in the column contains Text (Factor)?
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]]
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Marna Wagley
2018-Apr-18 18:25 UTC
[R] How to replace numeric value in the column contains Text (Factor)?
Hi David and Jeff, Both ways worked for my table. it helped me a lot (I was really struggling on it to change the values and thinking to do manually). You are great. Thank you On Wed, Apr 18, 2018 at 11:13 AM, David L Carlson <dcarlson at tamu.edu> wrote:> The simplest would be to convert precip to character and then back to a > factor if you really want it to be a factor. This will also remove the > levels that no longer exist. > > str(dat) > # 'data.frame': 5 obs. of 3 variables: > # $ Sites : Factor w/ 5 levels "Site1","Site2",..: 1 2 3 4 5 > # $ temp : num 14 15 12 12.5 17 > # $ precip: Factor w/ 5 levels "15","34","high",..: 3 4 5 2 1 > > dat$precip <- as.character(dat$precip) > dat[4:5, 3] <-"20" > dat$precip <- factor(dat$precip) > > str(dat) > # 'data.frame': 5 obs. of 3 variables: > # $ Sites : Factor w/ 5 levels "Site1","Site2",..: 1 2 3 4 5 > # $ temp : num 14 15 12 12.5 17 > # $ precip: Factor w/ 4 levels "20","high","low",..: 2 3 4 1 1 > > ---------------------------------------- > David L Carlson > Department of Anthropology > Texas A&M University > College Station, TX 77843-4352 > > -----Original Message----- > From: R-help <r-help-bounces at r-project.org> On Behalf Of Marna Wagley > Sent: Wednesday, April 18, 2018 12:56 PM > To: r-help mailing list <r-help at r-project.org> > Subject: [R] How to replace numeric value in the column contains Text > (Factor)? > > 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]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]