Create categories by comparing a numeric column with a fixed value
Try
iris$Regulation <- ifelse(iris$Sepal.Length >=5, "UP", "DOWN")
Add column with values depending on another column to a dataframe
You can nest ifelse
stataments. It's very handy when you are making categorical variables out of continuous ones.
data$c4 <- ifelse(data$c2 >= 0 & data$c2 <= 4, 'low',
ifelse(data$c2 >=5 & data$c2 <=9, 'medium',
ifelse(data$c2 >=10, 'High', 'something else')
matching values in different columns of a dataframe in r
A base R option
transform(
df,
C = c("miss","match")[(A==B)+1]
)
giving
A B C
1 a c miss
2 x x match
3 t q miss
4 l l match
5 w y miss
6 b b match
Data
> dput(df)
structure(list(A = c("a", "x", "t", "l", "w", "b"), B = c("c",
"x", "q", "l", "y", "b")), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6"))
Assign a Value based on the numbers in a separate columns in R
You can use a simple ifelse
:
df$new_col <- ifelse(df$likes > df$dislikes, 'positive', 'negative')
This can also be written without ifelse
as :
df$new_col <- c('negative', 'positive')[as.integer(df$likes > df$dislikes) + 1]
How to add column into a dataframe based on condition? (without ifelse)
The first part effectively creates an object with two entries: the potential outcomes DOWN/UP
. The second is a logical argument indexing one of those outcomes.
Might be easier if we create an object for the outcomes:
outcome <- c('DOWN','UP')
Square brackets index the object:
> outcome[ 2 ]
[1] "UP"
Inside the brackets is a logical statement returning FALSE/TRUE
. Focusing only on the first 5 observations:
> iris[1:5,'Sepal.Length'] >= 5
[1] TRUE FALSE FALSE FALSE TRUE
Which store numerically as 0/1
:
> as.numeric(iris[1:5,'Sepal.Length'] >= 5)
[1] 1 0 0 0 1
Addding 1 to the above returns 1s and 2s, which index either DOWN/UP
in the outcomes
object:
> condition[ (iris[1:5,'Sepal.Length'] >= 5) + 1 ]
[1] "UP" "DOWN" "DOWN" "DOWN" "UP"
Extract AM/PM from Time in R
You can remove everything after colon, convert data to integer and assign 'PM'
to all the values greater than 11 and "AM"
otherwise.
df <- data.frame(Time = c('0:00', '1:00', '2:00', '13:00', '14:00'))
df$AMPM <- ifelse(as.integer(sub(':.*', '', df$Time)) > 11, 'PM', 'AM')
#Without ifelse
#c('AM', 'PM')[(as.integer(sub(':.*', '', x)) > 11) + 1]
df
# Time AMPM
#1 0:00 AM
#2 1:00 AM
#3 2:00 AM
#4 13:00 PM
#5 14:00 PM
Related Topics
How to Install Development Version of R Packages Github Repository
How to Add a General Label to Facets in Ggplot2
Changing Facet Label to Math Formula in Ggplot2
How to Make Tibbles Display Significant Digits
Apply a Function to Every Row of a Matrix or a Data Frame
Add Error Bars to Show Standard Deviation on a Plot in R
Hosting and Setting Up Own Shiny Apps Without Shiny Server
A Matrix Version of Cor.Test()
Lib Unspecified & Error in Loadnamespace
How to Run R on a Server Without X11, and Avoid Broken Dependencies
Add Multiple Columns to R Data.Table in One Function Call
How to Update R Packages in Default Library on Windows 7
Split Date into Different Columns for Year, Month and Day
Shiny: Differencebetween Observeevent and Eventreactive
Using Gsub to Extract Character String Before White Space in R