Create a new variable based on the first 7 characters of existing variable
You are passing wrong argument to substr
Try
variable2 <- substr(mydata$variable,1,7)
Create new variable based on another variable that contrains character strings of numbers?
You can use the following:-
df$Relevant_Diagnoses <- ifelse(is.na(df$Diagnoses), NA,
ifelse(grepl("2", df$Diagnoses), 1, 0))
Create a new variable based in conditions in R
The syntax of mutate
is mutate(.data, new_col = something)
. Also, since you have multiple condition based on var1
, you'll need to have a nested ifelse
. Therefore, the correct way of doing it is:
library(dplyr)
df %>% mutate(new = ifelse(var1 == 1,
var2,
ifelse(var1 == 5,
var3,
ifelse(var1 == 10,
NA,
ifelse(var1 == 15,
var4,
NA)))))
Or use dplyr::case_when
, which is way clearer in the syntax:
df %>% mutate(new = case_when(var1 == 1 ~ as.character(var2),
var1 == 5 ~ var3,
var1 == 15 ~ var4))
Output
The output of the above two methods are the same.
var1 var2 var3 var4 new
1 1 45 AH67 A456 45
2 1 78 GH98 D788 78
3 5 46 GD94 M747 GD94
4 5 98 GF21 G589 GF21
5 10 47 GD09 I989 <NA>
6 10 54 KG32 U456 <NA>
7 15 48 FS89 C191 C191
8 15 66 GF23 Y198 Y198
In R: How to create new variables based on conditions on existing variables
You could create an index dataset ('df2') and merge that with the original dataset ('df1')
merge(df1, df2)
# var1 var2 var3 var4
#1 01 A New York Buffalo
#2 01 A New York Buffalo
#3 01 B New York Cornell
#4 02 C North Carolina Raleigh
#5 02 C North Carolina Raleigh
#6 03 D Texas Dallas
data
df1 <- structure(list(var1 = c("01", "01", "01", "02", "02", "03"),
var2 = c("A", "B", "A", "C", "C", "D")), .Names = c("var1",
"var2"), row.names = c(NA, -6L), class = "data.frame")
df2 <- data.frame(var1=c('01', '01', '02', '03'), var2=LETTERS[1:4],
var3=c('New York', 'New York', 'North Carolina', 'Texas'),
var4=c('Buffalo', 'Cornell', 'Raleigh', 'Dallas'))
creating a new variable according to existing variables using R
ifelse
evaluates a condition (whether or not temp
is NA) in an element by element fashion. We will test whether or not temp is NA and assign the resulting value as NA or surv1 depending on the outcome.
data$surv2 <- with(data, ifelse( is.na(temp), NA, surv1))
Related Topics
How to Calculate Confidence Intervals for Nonlinear Least Squares in R
How to Preserve Continuous (1,2,3,...N) Ranking Notation When Ranking in R
Coerce Logical (Boolean) Vector to 0 and 1
Splitting String Between Capital and Lowercase Character in R
Removing One Table from Another in R
How to Get the Second Sub Element of Every Element in a List
Rbuildignore and Excluding Directories
Is There a Table or Catalog of Aesthetics for Ggplot2
What's the Difference Between Substitute and Quote in R
Extract Last Non-Missing Value in Row with Data.Table
Large Integers in Data.Table. Grouping Results Different in 1.9.2 Compared to 1.8.10
Get Names of Column with Max Value for Each Row
Reversing Y-Axis in an Individual Ggplot Facet
R Ggplot2: Using Stat_Summary (Mean) and Logarithmic Scale
R - Unable to Install R Packages - Cannot Open the Connection
Higher Level Functions in R - Is There an Official Compose Operator or Curry Function