Re: [R] Recoding factor labels that are lists into first element of list

2009-12-11 Thread Gabor Grothendieck
Or this which removing the comma and everything thereafter in each level that has a comma: levels(x$a) <- sub(",.*", "", levels(x$a)) On Fri, Dec 11, 2009 at 5:21 AM, jim holtman wrote: > try this: > >> x <- data.frame(a=c('cat', 'cat,dog', 'dog', 'dog,cat')) >> x >        a > 1     cat > 2 cat,

Re: [R] Recoding factor labels that are lists into first element of list

2009-12-11 Thread jim holtman
try this: > x <- data.frame(a=c('cat', 'cat,dog', 'dog', 'dog,cat')) > x a 1 cat 2 cat,dog 3 dog 4 dog,cat > levels(x$a) [1] "cat" "cat,dog" "dog" "dog,cat" > # change the factors > x$a <- factor(sapply(strsplit(as.character(x$a), ','), '[[', 1)) > x a 1 cat 2 cat 3 dog

[R] Recoding factor labels that are lists into first element of list

2009-12-10 Thread Jennifer Walsh
Hi all, I've Googled far and wide but don't think I know the correct terms to search for to find an answer. I have a massive dataset where one of the factors is made up of both individual items and lists of items (for example, "cat" and "cat, dog, bird"). I would like to recode this facto