Change specific row name in a specific column - r

I want to change the name of a specific row in a specific column. I have troubles navigating around in my dataframe to change the name of this specific object.
For example: I want to change "RowName1" in the column "ColumnName1"
Thank you in advance!

Okay I found what I was looking for:
df[df == "OldValue"] <- "NewValue"

Related

Combine lapply and gsub to replace a list of values for another list of values

I am currently looking for a way to simplify searching through a column within a dataframe for a vector of values and replacing each of of those values with another value (also contained within a separate vector). I can run a for loop for this, but it must be possible within the apply family, I'm just not seeing it yet. Very new to using the apply family and could use help.
So far, I've been able to have it replace all instances of the first value in my vector with the new first value in the new vector, it just isn't iterating past the first level. I hope this makes sense. Here is the code I have:
#standardize tank location
old_tank_list <- c("7.C.4","7.C.5","7.C.6","7.C.7","7.C.8","7.C.9","7.C.10","7.C.11")
new_tank_list <- c("7.B.3-4","7.C.3-4","7.C.1-2","7.C.5-6","7.C.7-8","7.C.9-10","7.E.9-10","7.C.11-12")
sapply(df_growth$Tank,function(y) gsub(old_tank_list,std_tank_list,y))
Tank is the name of the column I am trying to replace all of these values within. I haven't assigned it back yet, because I want to test the functionality first. Thanks for any help you can offer.
Hopefully, this image will help. The photo on the left is the column before my function is applied. The column on the right is after. Basically, I just want to batch change text values.
Before and After
library(dplyr)
df %>%
mutate(Tank = recode(Tank, !!!setNames(new_tank_list, old_tank_list)))

How to get specific value from data frame according other variable in R?

I have an "issue" variable that will change according to user input, for example I choose Unbonded.
And I make "currentdata" variable to get data from database like this.
issue <- "Unbonded"
currentdata <- dbGetQuery(con,paste0("SELECT TOP 1 WITH TIES
Unbonded,
Clogged,
Dirty,
Leaking,
Others
FROM Table_Analytic
ORDER BY Table_Analytic.ID DESC;"))
currentissue <- currentdata$ ????
How can I get only the values ​​that match with the issue to put into the "currentissue" variable
Thankyou
You can use the following code:
currentissue <- currentdata[[issue]]

Adressing columns based on only parts of the name in order to simplify lines

My first question here and I am not very experienced, however I hope this question is easy enough to answer since I only want to know if what I describe in the title is possible.
I have multiple dataframes taken from online capacity tests participants did.
For all Items I have response, score, and durationvariables among others.
Now I want to delete rows where all responsevariables are NA. So I can't just use a command to delete rows with where all is NA but there are also to many columns to do it by hand. And I also want to keep the dataframe together while doing it in order to really drop the complete rows, so just extracting all responsevariables doesn't sound like a good option.
However, besides a 3digit number based on the specific items the responsevariablenames are basically the same.
So instead of writing a very long impractical line mentioning all responsevariables and to drop the row if they all contain NA is there a way to not use the full anme of a variable but only use the end of the name for example so R checks the condition for all variables ending that way?
simplified e.g: instead of
newdf <- olddf[!(olddf$item123response != NA & olddf$item131response != NA & etc),]
Can I just do something like newdf <- olddf[!(olddf$xxxresponse != NA),] ?
I tried to google an answer but I didn't know how to frame my question effectively.
Thanks in advance!
Try This
newdf <- olddf[complete.cases(olddf[, grep('response', names(olddf))]), ]

How to change the value in a group of cells?

I'm wondering if it is possible to change the contents of multiple cells in a table using R?
Consider this example: Example
I need to change the values 'Femini.' to 'Feminine'. The problem is that i have a great number of cells to change... Is there some command that help me doing this?
Thanks for the help,
Luís
Say your dataframe is called df
df$Genre[df$Genre == 'Femini'] <- 'Feminine'

Adding based on column name not number

I am adding together columns with the following code:
cbind(rowSums(data[,c(5,10,11,15)],na.rm=TRUE),rowSums(data[,c(3,6,7)],na.rm=TRUE))
But I want to add the columns based upon the column name not their number to eliminate confusion....
Is there an easy way to do this?
Yep, just replace the numbers with the column names in quotes:
cbind(rowSums(data[,c('elephant','giraffe')],na.rm=TRUE),
rowSums(data[,c('fish','penguin','albatross')],na.rm=TRUE))
I'm collecting data on animal species today. Not sure why I'm adding them together.

Resources