Reputation: 69
I have a column in my dataframe and this is bit odd.
when I do a class(df$Contributor) it shows that its a dataframe , dataframe whithin a dataframe ?? :(
> class(df$Contributor)
[1] "data.frame"
>
Is there any way I could change this to a factor ?
Here's a partial output from str(df1)
$ ArticleId : Factor w/ 186 levels "10.1002/ajh.26",..: 167 24 130 136 5 15 20 179 175 70 ...
$ Volume : Factor w/ 93 levels "10","100","103",..: NA 12 NA 67 NA NA NA NA NA 18 ...
$ Issue : Factor w/ 14 levels "1","10","11",..: NA 11 NA 9 NA NA NA NA NA 8 ...
$ Members :'data.frame': 186 obs. of 1 variable:
..$ Contributor: chr "Ellis Johnson A, Hernandez John , Erin Martin D, Doe Jane M" "Ibrahim Joseph A" "Tenzen E M, Zhao V, Jahn T, Lee S J, McCarthy P L, Izzo J D, Romi M, Drexler R, Dlesch S, Tim H, Atamandi N, Lee E, Persuzo"| __truncated__ "Croso, Tim Zi, Gram Lindsey R, Jackson Samuel J, Friedman Tom A, Johnson Boris J, Johnson Alan "| __truncated__ ...
I tried doing
df$Contributor <- as.factor(df$Contributor)
I got an error
Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?
Upvotes: 0
Views: 139
Reputation: 38520
The following should also work:
d$z <- as.factor(unlist(d$z))
At least it did on @joran s example.
This will not work if the data.frame z has more than one column, but you will get an error indicating non-conforming sizes, which is reassuring.
Upvotes: 2
Reputation: 173737
You could try:
df$new_col <- as.factor(df$Members$Contributor)
and then remove the "bad" column via df$Members <- NULL
.
You may have accidentally created this problem by doing something like:
df$Members <- Members
where Members
was a one column data frame with a column called Contributor
. For example,
d <- data.frame(x = 1:5,y = 1:5)
d$z <- data.frame(z1 = letters[1:5])
> d
x y z1
1 1 1 a
2 2 2 b
3 3 3 c
4 4 4 d
5 5 5 e
> str(d)
'data.frame': 5 obs. of 3 variables:
$ x: int 1 2 3 4 5
$ y: int 1 2 3 4 5
$ z:'data.frame': 5 obs. of 1 variable:
..$ z1: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5
Upvotes: 3