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)))
I need to use R to print dataset of a dataframe so that columns are in alphabetical order. It sounds sorting column name is required. I tried sort (data.frame$) but it didn't work. Can anybody help me?
You can use the code below
df[order(names(df))]
or
df[sort(names(df))]
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"
I have a similar table as shown in this example (mine has more variables but the problem remains the same). I need observations taken in the same counties to be given the same index, and stored in J
I need to create the vector J as shown on the right hand side of the picture, can anybody help me with this? The table can be found here. Thanks.
It appears that you would just use your county column?
j = randomdata$county which you can subset as j[919] and get 85.
I have a large data set titled credit_long with one of the columns being DEFAULT, I want to know how many are yes and how many are no. I have not been able to solve this because the column is set up as yes/no rather than numeric.
I tried using
as.numeric(as.factor(credit_long$DEFAULT))
with no luck.
As #Gregor commented, using:
table(credit_long$DEFAULT)
will give you counts for each available option. To convert this to a percentage you could do:
table(credit_long$DEFAULT)[1]/nrow(credit_long)
table(credit_long$DEFAULT)[2]/nrow(credit_long)