Re: [R] change value in one cell
That's great! But, I guess I should have provided a better example for my problem. The reason being that I did not consider strings. For instance, in my case, I'm actually trying to add a value to a data frame that looks like this: Obs X 0 NA 1 01 001 2 01 002 3 01 003 And I am actually trying to get that specific NA value and convert it to 00 000 so it can look like this: Obs X 0 00 000 1 01 001 2 01 002 3 01 003 When I write the code you provided, I get this result: x[1,X]-00 000 Warning message: In `[-.factor`(`*tmp*`, iseq, value = c(NA, 3L, 4L, 5L, 6L, 7L, : invalid factor level, NAs generated I am wondering what I am doing wrong and how to solve this problem. Thanks for all the help! Carlos On 6/10/2011 8:34 PM, jholtman [via R] wrote: you probably need to read the Introduction to R to understand indexing: x Obs X Y Z 1 1 1 0 1 2 2 0 0 1 3 3 1 1 1 4 4 0 1 1 5 5 1 1 0 6 6 0 0 0 x[4, Y] - 0 x Obs X Y Z 1 1 1 0 1 2 2 0 0 1 3 3 1 1 1 4 4 0 0 1 5 5 1 1 0 6 6 0 0 0 On Fri, Jun 10, 2011 at 5:42 PM, jour4life [hidden email] /user/SendEmail.jtp?type=nodenode=3589793i=0 wrote: Hello all, I am wondering if there is a way to change the value of one cell in R. For instance let's say I have a hypothetical data frame that looks like this: Obs X Y Z 11 0 1 20 0 1 31 1 1 40 1 1 51 1 0 60 0 0 I would like to change the value of the 4th observation in the Y column from 1 to 0. It should look like this: Obs X Y Z 11 0 1 20 0 1 31 1 1 40 0 1 51 1 0 60 0 0 Is it possible to change the value in one command? Thanks, Carlos -- View this message in context: http://r.789695.n4.nabble.com/change-value-in-one-cell-tp3589456p3589456.html Sent from the R help mailing list archive at Nabble.com. __ [hidden email] /user/SendEmail.jtp?type=nodenode=3589793i=1 mailing list 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. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? __ [hidden email] /user/SendEmail.jtp?type=nodenode=3589793i=2 mailing list 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. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/change-value-in-one-cell-tp3589456p3589793.html To unsubscribe from change value in one cell, click here http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_codenode=3589456code=am91cjRsaWZlQGdtYWlsLmNvbXwzNTg5NDU2fC0xNjIzNjcwNDM1. -- View this message in context: http://r.789695.n4.nabble.com/change-value-in-one-cell-tp3589456p3590037.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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.
Re: [R] change value in one cell
On 11/06/11 18:14, jour4life wrote: That's great! But, I guess I should have provided a better example for my problem. The reason being that I did not consider strings. For instance, in my case, I'm actually trying to add a value to a data frame that looks like this: Obs X 0 NA 1 01 001 2 01 002 3 01 003 And I am actually trying to get that specific NA value and convert it to 00 000 so it can look like this: Obs X 0 00 000 1 01 001 2 01 002 3 01 003 When I write the code you provided, I get this result: x[1,X]-00 000 Warning message: In `[-.factor`(`*tmp*`, iseq, value = c(NA, 3L, 4L, 5L, 6L, 7L, : invalid factor level, NAs generated I am wondering what I am doing wrong and how to solve this problem. Thanks for all the help! The error is clear enough, isn't it? 00 000 is not one of the levels of the X column of your data frame; this column is a factor. Probably because of the stringsAsFactors = TRUE default in options(). Since you appear to have been unaware of the factor nature of X, presumably you don't really want it to be a factor. If this is the case execute x[,X] - as.character(x[,X]) and then your reassignment of the [1,X] entry of x will work. If you do want X to be a factor you could: (a) execute x[,X] - factor(x[,X]) *after* doing the reassignment, or (b) execute levels(x[,X]) - c(00 000,levels(x[,X])) *before* doing the reassignment. Learn more about how R works. In particular learn about factors; they are important and useful. cheers, Rolf Turner __ R-help@r-project.org mailing list 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.
Re: [R] change value in one cell
your dataframe has X as a factor; convert to character vector and try again. x$X - as.character(x$X) Sent from my iPad On Jun 11, 2011, at 2:14, jour4life jour4l...@gmail.com wrote: That's great! But, I guess I should have provided a better example for my problem. The reason being that I did not consider strings. For instance, in my case, I'm actually trying to add a value to a data frame that looks like this: Obs X 0 NA 1 01 001 2 01 002 3 01 003 And I am actually trying to get that specific NA value and convert it to 00 000 so it can look like this: Obs X 0 00 000 1 01 001 2 01 002 3 01 003 When I write the code you provided, I get this result: x[1,X]-00 000 Warning message: In `[-.factor`(`*tmp*`, iseq, value = c(NA, 3L, 4L, 5L, 6L, 7L, : invalid factor level, NAs generated I am wondering what I am doing wrong and how to solve this problem. Thanks for all the help! Carlos On 6/10/2011 8:34 PM, jholtman [via R] wrote: you probably need to read the Introduction to R to understand indexing: x Obs X Y Z 1 1 1 0 1 2 2 0 0 1 3 3 1 1 1 4 4 0 1 1 5 5 1 1 0 6 6 0 0 0 x[4, Y] - 0 x Obs X Y Z 1 1 1 0 1 2 2 0 0 1 3 3 1 1 1 4 4 0 0 1 5 5 1 1 0 6 6 0 0 0 On Fri, Jun 10, 2011 at 5:42 PM, jour4life [hidden email] /user/SendEmail.jtp?type=nodenode=3589793i=0 wrote: Hello all, I am wondering if there is a way to change the value of one cell in R. For instance let's say I have a hypothetical data frame that looks like this: Obs X Y Z 11 0 1 20 0 1 31 1 1 40 1 1 51 1 0 60 0 0 I would like to change the value of the 4th observation in the Y column from 1 to 0. It should look like this: Obs X Y Z 11 0 1 20 0 1 31 1 1 40 0 1 51 1 0 60 0 0 Is it possible to change the value in one command? Thanks, Carlos -- View this message in context: http://r.789695.n4.nabble.com/change-value-in-one-cell-tp3589456p3589456.html Sent from the R help mailing list archive at Nabble.com. __ [hidden email] /user/SendEmail.jtp?type=nodenode=3589793i=1 mailing list 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. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? __ [hidden email] /user/SendEmail.jtp?type=nodenode=3589793i=2 mailing list 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. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/change-value-in-one-cell-tp3589456p3589793.html To unsubscribe from change value in one cell, click here http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_codenode=3589456code=am91cjRsaWZlQGdtYWlsLmNvbXwzNTg5NDU2fC0xNjIzNjcwNDM1. -- View this message in context: http://r.789695.n4.nabble.com/change-value-in-one-cell-tp3589456p3590037.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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. __ R-help@r-project.org mailing list 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.
Re: [R] change value in one cell
you probably need to read the Introduction to R to understand indexing: x Obs X Y Z 1 1 1 0 1 2 2 0 0 1 3 3 1 1 1 4 4 0 1 1 5 5 1 1 0 6 6 0 0 0 x[4, Y] - 0 x Obs X Y Z 1 1 1 0 1 2 2 0 0 1 3 3 1 1 1 4 4 0 0 1 5 5 1 1 0 6 6 0 0 0 On Fri, Jun 10, 2011 at 5:42 PM, jour4life jour4l...@gmail.com wrote: Hello all, I am wondering if there is a way to change the value of one cell in R. For instance let's say I have a hypothetical data frame that looks like this: Obs X Y Z 1 1 0 1 2 0 0 1 3 1 1 1 4 0 1 1 5 1 1 0 6 0 0 0 I would like to change the value of the 4th observation in the Y column from 1 to 0. It should look like this: Obs X Y Z 1 1 0 1 2 0 0 1 3 1 1 1 4 0 0 1 5 1 1 0 6 0 0 0 Is it possible to change the value in one command? Thanks, Carlos -- View this message in context: http://r.789695.n4.nabble.com/change-value-in-one-cell-tp3589456p3589456.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? __ R-help@r-project.org mailing list 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.