0

I'm looking to load and process a CSV file with seven variables, one which is a grouping variable / factor (data$hashtag) and six which are categories (data$support and others) denoted with either an "X" or "x" (or were left blank).

data <- read.csv("maet_coded_tweets.csv", stringsAsFactors = F)

names(data) <- c("hashtag", "support", "contributeConversation", "otherCommunities", "buildCommunity", "engageConversation", "unclear")

str(data)

'data.frame':   854 obs. of  7 variables:
 $ hashtag               : chr  "#capstoneisfun" "#capstoneisfun" "#capstoneisfun" "#capstoneisfun" ...
 $ support               : chr  "x" "x" "x" "x" ...
 $ contributeConversation: chr  "" "" "" "" ...
 $ otherCommunities      : chr  "" "" "" "" ...
 $ buildCommunity        : chr  "" "" "" "" ...
 $ engageConversation    : chr  "" "" "" "" ...
 $ unclear               : chr  "" "" "" "" ...

When I use a function to recode "X" or "x" to 1, and "" (blank) 0, the data are strangely character type, not numeric as intended.

recode <- function(x) {

  x[x=="x"] <- 1
  x[x=="X"] <- 1
  x[x==""] <- 0
  x
}

data[] <- lapply(data, recode)

str(data)

'data.frame':   854 obs. of  7 variables:
 $ hashtag               : chr  "#capstoneisfun" "#capstoneisfun" "#capstoneisfun" "#capstoneisfun" ...
 $ support               : chr  "1" "1" "1" "1" ...
 $ contributeConversation: chr  "0" "0" "0" "0" ...
 $ otherCommunities      : chr  "0" "0" "0" "0" ...
 $ buildCommunity        : chr  "0" "0" "0" "0" ...
 $ engageConversation    : chr  "0" "0" "0" "0" ...
 $ unclear               : chr  "0" "0" "0" "0" ...

When I tried to coerce the characters using as.numeric() in the function, it still didn't work. What gives - why would the variables be treated as characters and how to character variables to numeric?

3
  • 1
    vectors can only hold one datatype. Therefore, if you replace parts of a characterstring with a numeric variable it gets converted to character. How exactly did you use as.numeric() in the function? Commented Nov 12, 2015 at 13:39
  • recode <- function(x) { x[x=="x"] <- as.numeric(1) x[x=="X"] <- as.numeric(1) x[x==""] <- as.numeric(0) x } Commented Nov 12, 2015 at 13:41
  • You could try return(as.numeric(x)). As I said in my previous comment, the way you did that still forces conversion to character. Or you could do res <- ifelse(x %in% c("x","X"),1,0) Commented Nov 12, 2015 at 13:41

3 Answers 3

2

How about:

recode <- function(x) {
  ifelse(x %in% c('X','x'), 1,0)
}

Explanation: the steps in the function are evaluated sequentially, not simultaneously. So when you partially assign 1's to a character vector, these get converted to "1"s.

Sign up to request clarification or add additional context in comments.

3 Comments

For further practice, you can also convert logical to 0/1 with as.numeric(). So you could do recode2 <- function(x) as.numeric(x %in% c('X','x')) or even recode3 <- function(x) as.numeric(grepl('^x$',x, ignore.case=T))
I'd add as.integer(tolower(x) == "x")
@docendodiscimus true, just a note that OP seemed to want numeric. however, a bool seems more logical :) than an int or float
1

Waht about something like this?

# sample data with support being a character vector
data.frame(support = c("X","X","0","x","0"),a=1:5,stringsAsFactors = F)->myDat
# convert to a factor and check the order of the levels
myDat$support <- as.factor(myDat$support)
levels(myDat$support)
#"0" "x" "X"
# just to see that it worked make an additional variable
myDat$supportrecoded <- myDat$support
# change levels and convert
levels(myDat$supportrecoded) <- c("0","1","1")
myDat$supportrecoded <- as.integer(as.character(myDat$supportrecoded ))

Comments

1

Using mapvalues from plyr.

data$support <- as.numeric(mapvalues(data$support, c("X", "x", ""), c(1, 1, 0)))

Using replace.

data$support <- replace(x <- data$support, x == "X", 1)
data$support <- replace(x <- data$support, x == "x", 1)
data$support <- replace(x <- data$support, x == "", 0)
data$support <- numeric(data$support)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.