Attempt to change cell value fails

4 Aufrufe
Direkt zur ersten ungelesenen Nachricht

Kristina Michelle Wolf

ungelesen,
22.03.2017, 18:47:1122.03.17
an davi...@googlegroups.com

Hi all!

 

Quick question, and not sure if I need to provide reproducible example or not, but hoping not.

 

I am looking for the following values in a dataframe:

 

tomato_analyses[which(tomato_analyses$plot == "6-9" & tomato_analyses$harvest_year == "2001"),] #row 925 value

 

I get this:

 

     plot plot_side analysis_date     sample_type tomato_part       tomato_analysis analysis_value units comments harvest_year
925   6-9         W    2001-08-24    ptab_samples       fruit    Color score - PTAB         25.000                        2001

 

I want the value in the row 925, seventh column (analysis_value) to be changed to 27, not 25.

 

I do this:

 

tomato_analyses[925, 7] = 27

 

And when I go back to check again, I get the same thing; nothing changed.  

 

     plot plot_side analysis_date     sample_type tomato_part       tomato_analysis analysis_value units comments harvest_year
925   6-9         W    2001-08-24    ptab_samples       fruit    Color score - PTAB         25.000                        2001

 

 

Oddly enough, this works FINE for other rows. What is happening? Any ideas?

 

Thanks!

Michael Koontz

ungelesen,
22.03.2017, 19:03:2122.03.17
an davi...@googlegroups.com
Hi Kristina,

Is it possible that the rownames of your data.frame don’t exactly match up to a sequence from 1 to the total number of rows? That is, “925” is a label for that row and doesn’t necessarily mean that the row you printed out is the 925th row of the data.frame. This can sometimes happen if some rows were deleted or reordered.

What happens if you run this line instead to change that cell in the data.frame by using the conditional statements to access it?

tomato_analyses[which(tomato_analyses$plot == "6-9" & tomato_analyses$harvest_year == "2001”), “analysis_value”] <- 27

And then look to see what happened:

tomato_analyses[which(tomato_analyses$plot == "6-9" & tomato_analyses$harvest_year == "2001”), ]

If that doesn’t work, what does tomato_analyses[925, ] return?

Mike

--
Michael Koontz — website

Graduate Group in Ecology
University of California, Davis
Davis, CA 95616





-- 
Check out our R resources at http://d-rug.github.io/
--- 
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at https://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.

Kristina Michelle Wolf

ungelesen,
22.03.2017, 19:12:1122.03.17
an davi...@googlegroups.com

I can imagine it is an issue from my deleting and/or reordering, which I have been doing, but I have been checking each time for the new row number, and it doesn’t seem to consistently give me the updated row number as I go through and make multiple changes.

 

I can’t use the suggested first line because there are other rows that match that same call, and I don’t want to change ALL of them, only one of them.

 

How can I ensure that I am getting the right row number returned when I use which()?

Noam Ross

ungelesen,
22.03.2017, 20:05:2222.03.17
an davi...@googlegroups.com

It might make sense to add an index column that can resolve these ambiguities:

tomato_analyses[["index"]] <- 1:nrow(tomato_analyses)

Then you can use this value for future lookups. This will give you the rows, but now also show index:

tomato_analyses[tomato_analyses$plot == "6-9" & tomato_analyses$harvest_year == "2001",]

(Note you don’t need which() here - the bracket indexing can go on just the TRUE/FALSE values)

Now, replace values using the index:

tomato_analyses[tomato_analyses$index==INDEX_PROBABLY_NOT_925,] <- 27

Michael Koontz

ungelesen,
22.03.2017, 20:18:0422.03.17
an davi...@googlegroups.com
I like Noam’s idea of using an explicit identifier for each row by adding a new column called “index.”

You could also use the rownames() function as part of your conditional statement if you don’t want to add a column for some reason:

tomato_analyses[tomato_analyses$plot == "6-9" & tomato_analyses$harvest_year == "2001" & rownames(tomato_analyses == "925"), ]

In contrast to Noam (eep!), I would recommend continuing to use the which() function unless you are super duper sure that there are no NA’s in the column you are using in your conditional statement. When you use a conditional statement (which returns TRUE/FALSE) and there are NA’s in that column, you’ll end up with some probably undesirable behavior.

For instance:

iris # Built in data frame that looks good!
iris[iris$Sepal.Length > 5, ] # all of the columns in the iris data.frame but only the rows in which Sepal.Length is greater than 5; not using which() so the conditional statement returns a "logical indexing vector" of TRUEs and FALSEs
iris[which(iris$Sepal.Length > 5), ] # same subset as above, but using which() so the index numbers of the desired rows are returned

iris$Sepal.Length[c(3:6, 10:15)] <- NA # Arbitrarily make a few entries NA for demonstration

iris # Now there are some NAs
iris[iris$Sepal.Length > 5, ] # Aaaaaah! Trying to use the conditional statement without a which() when there are NAs
iris[which(iris$Sepal.Length > 5), ] # Probably the subset you are looking for

Michael Koontz

ungelesen,
22.03.2017, 20:19:4922.03.17
an davi...@googlegroups.com
Whoops, typo. I misplaced a parenthesis. The rownames() function is called on the data.frame and the character vector that is returned is compared to “925"

tomato_analyses[tomato_analyses$plot == "6-9" & tomato_analyses$harvest_year == "2001" & rownames(tomato_analyses) == "925", ]

M
Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten