-1

enter image description hereI should develop a model to measure the relationship between insecurity and authoritarianism. The problem is that in my code-book the item is as follow: are security concerns increased?

fallen a lot  
fallen a little  
stayed the same  
increased a little  
increased a lot  

Now I want to codify them into numbers as follow

fallen a lot=-2
fallen a little=-1
stayed the same=0
increased a little=1
increased a lot=2

don't know = NA

v22g is the interested column

dput(df2$v22g[1:30])


c("fallen a lot", "fallen a little", "stayed the same", "increased little", 
"increased a lot", "fallen a lot", "fallen a little", "stayed the same", 
"increased little", "increased a lot", "fallen a lot", "fallen a little", 
"stayed the same", "increased little", "increased a lot", "fallen a lot", 
"fallen a little", "stayed the same", "increased little", "increased a lot", 
"fallen a lot", "fallen a little", "stayed the same", "increased little", 
"increased a lot", "fallen a lot", "fallen a little", "stayed the same", 
"increased little", "increased a lot")

someone can tell me how can I do this? thank u

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
Luke
  • 21
  • 5
  • Can you post sample data? Please edit **the question** with the output of `dput(df)`. Or, if it is too big with the output of `dput(head(df, 20))`. (`df` is the name of your dataset.) – Rui Barradas Dec 24 '19 at 13:19
  • Also, what have you tried? In order to ask a better question please read [How to ask a good question](https://stackoverflow.com/help/how-to-ask) and [Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve) and [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Rui Barradas Dec 24 '19 at 13:19
  • https://www.dropbox.com/s/233qf4hbvczumr1/Screenshot%202019-12-24%2014.25.05.png?dl=0 in column v22g, I want to transform everything into numbers as i've stated before – Luke Dec 24 '19 at 13:27
  • Images are a really bad way of posting data (or code). Please edit **the question** with the output of `dput(df$v22g[1:30])`. And are the `"don't know"` values missing values? – Rui Barradas Dec 24 '19 at 13:55
  • yes don't know i compute as NA – Luke Dec 24 '19 at 14:00

2 Answers2

1

you could just define a function (here called "numerify" where you put in the specific string and that spits out the respective number).

numerify <- function(ranked){
  switch(ranked,
    "fallen a lot" = -2,
    "fallen a little" = -1,
    "stayed the same"= 0,
    "increased a little" = 1,
    "increased a lot" = 2,
    "don't know" = NA
  )
}

numerify("fallen a lot")

thanks to Rui Barradas for the addition: here is a test data.frame (df) with the column "v22g" that needs numerification. The second line adds a column that contains the numerics.

df <- data.frame(v22g=c("fallen a lot", "stayed the same"))
df$numbers <- sapply(as.character(df$v22g), numerify)
df
NicolasH2
  • 774
  • 5
  • 20
0

This solution coerces to class "factor" first, with the factor levels in the required order, and then to integer.

levs_v22g <-
c("fallen a lot", 
  "fallen a little", 
  "stayed the same", 
  "increased a little", 
  "increased a lot", 
  "don't know")

df$v22gPoints <- factor(df$v22g, levels = levs_v22g)
df$v22gPoints <- as.integer(df$v22gPoints) - 3
is.na(df$v22gPoints) <- df$v22g == "don't know"

head(df, 10)
                 v22g v22gPoints
#1  increased a little          1
#2     fallen a little         -1
#3          don't know         NA
#4     increased a lot          2
#5  increased a little          1
#6        fallen a lot         -2
#7     increased a lot          2
#8          don't know         NA
#9  increased a little          1
#10    fallen a little         -1

Data creation code.

set.seed(1234)
n <- 30
v22g <- sample(levs_v22g, n, TRUE)
df <- data.frame(v22g)
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66