Muneer
Muneer

Reputation: 219

Converting two column vectors of a data frame into a single numeric column

Consider the following toy data frame of my seed study:

site <- c(LETTERS[1:12])          
site1 <- rep(site,each=80)

fate <- c('germinated', 'viable', 'dead')
fate1 <- rep(fate,each=320)

number <- c(41:1000)

df <- data.frame(site1,fate1,number)

> str(df)
'data.frame':   960 obs. of  3 variables:
 $ site1 : Factor w/ 12 levels "A","B","C","D",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ fate1 : Factor w/ 3 levels "dead","germinated",..: 2 2 2 2 2 2 2 2 2 2 ...
 $ number: int  41 42 43 44 45 46 47 48 49 50 ...

I want R to go through all observations which are "dead" and assign "0" to every single one of them. Similarly, I want to assign "1" to all "viable" observations and "2" to all "germinated" observations.

My final data frame would be a single column, somewhat like this:

> year16
  [1] 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0
 [38] 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1

All suggestions are highly welcome

Upvotes: 0

Views: 64

Answers (3)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521259

Using case_when from the dplyr library:

df$year16 <-
case_when(
    levels(df$fate1)[df$fate1] == "dead" ~ 0,
    levels(df$fate1)[df$fate1] == "viable" ~ 1,
    levels(df$fate1)[df$fate1] == "germinated" ~ 2,
    TRUE ~ -1
)

Note: The solutions given by @David and @kath are much more graceful than this, but what I gave above would still work even if we had non numerical replacements.

Upvotes: 2

kath
kath

Reputation: 7724

As zx8754 mentioned, you can have a look at the properties of a factor.

year16 <- as.numeric(factor(df$fate1, levels = c("dead", "viable", "germinated")))-1

Here first I reorder the levels of df$fate1, so dead is assigned to 1, viable to 2 and germinated to 3. You want to start the sequence at 0, so I have to substract 1 after turning the factor in a numeric variable.

Upvotes: 3

Ollie Perkins
Ollie Perkins

Reputation: 343

Base R solution:

assignnum <- function(x) {

  if (x == 'viable') {
    z <- 1
} else if (x == 'dead') {
  z <- 0
} else if (x == 'germinated') {
  z <- 2  
}
  return(z)
}

df['result'] <- sapply(df$fate1, assignnum)

Upvotes: 0

Related Questions