problems with converting class to factor in dataframe [closed]
I am trying to combine dataframes and unify the classes of columns between them.
I have a problem with note being about to convert one column to a factor.
I've tried converting the dataframe using as.dataframe and converting the column using as.factor.
I have dataframes a,b,c and list
b$cat2 <-list[match (b$` Description`, list$Var1),"cat2",]
b2 <-b[,c("Number", "DOB", "Gender", "cat2"),]
colnames (b2) <- c ("Number", "DOB","Gender", "group" )
b2 <- as.data.frame (b2)
a$group <- as.factor (a$group)
class (b2$group )
[1] "tbl_df" "tbl" "data.frame"
b2$group <- as.factor (b2$group)
Error: Can't use matrix or array for column indexing
Call `rlang::last_error()` to see a backtrace
#combine a and b2
combo <- rbind (a, b2)
Warning message:
In `[<-.factor`(`*tmp*`, ri, value = list(cat2 = c(1, 0, 1, 1, 0, :
invalid factor level, NA generated
r dataframe
closed as off-topic by phiver, PoGibas, Rui Barradas, MLavoie, Billal Begueradj Jan 1 at 3:16
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – phiver, PoGibas, Rui Barradas, MLavoie, Billal Begueradj
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I am trying to combine dataframes and unify the classes of columns between them.
I have a problem with note being about to convert one column to a factor.
I've tried converting the dataframe using as.dataframe and converting the column using as.factor.
I have dataframes a,b,c and list
b$cat2 <-list[match (b$` Description`, list$Var1),"cat2",]
b2 <-b[,c("Number", "DOB", "Gender", "cat2"),]
colnames (b2) <- c ("Number", "DOB","Gender", "group" )
b2 <- as.data.frame (b2)
a$group <- as.factor (a$group)
class (b2$group )
[1] "tbl_df" "tbl" "data.frame"
b2$group <- as.factor (b2$group)
Error: Can't use matrix or array for column indexing
Call `rlang::last_error()` to see a backtrace
#combine a and b2
combo <- rbind (a, b2)
Warning message:
In `[<-.factor`(`*tmp*`, ri, value = list(cat2 = c(1, 0, 1, 1, 0, :
invalid factor level, NA generated
r dataframe
closed as off-topic by phiver, PoGibas, Rui Barradas, MLavoie, Billal Begueradj Jan 1 at 3:16
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – phiver, PoGibas, Rui Barradas, MLavoie, Billal Begueradj
If this question can be reworded to fit the rules in the help center, please edit the question.
3
The issue would be that thegroup
column in 'b2' is itself a data.frame/tbl_df.. If it is a single column dataframe, try ``b2$group <-factor( unlist(b2$group))
– akrun
Dec 31 '18 at 16:42
Fantastic! Appreciate your help so much!
– sar
Dec 31 '18 at 16:56
If you could edit this into a much simpler example that only illustrates the problem that would be very helpful.
– Elin
Dec 31 '18 at 17:46
add a comment |
I am trying to combine dataframes and unify the classes of columns between them.
I have a problem with note being about to convert one column to a factor.
I've tried converting the dataframe using as.dataframe and converting the column using as.factor.
I have dataframes a,b,c and list
b$cat2 <-list[match (b$` Description`, list$Var1),"cat2",]
b2 <-b[,c("Number", "DOB", "Gender", "cat2"),]
colnames (b2) <- c ("Number", "DOB","Gender", "group" )
b2 <- as.data.frame (b2)
a$group <- as.factor (a$group)
class (b2$group )
[1] "tbl_df" "tbl" "data.frame"
b2$group <- as.factor (b2$group)
Error: Can't use matrix or array for column indexing
Call `rlang::last_error()` to see a backtrace
#combine a and b2
combo <- rbind (a, b2)
Warning message:
In `[<-.factor`(`*tmp*`, ri, value = list(cat2 = c(1, 0, 1, 1, 0, :
invalid factor level, NA generated
r dataframe
I am trying to combine dataframes and unify the classes of columns between them.
I have a problem with note being about to convert one column to a factor.
I've tried converting the dataframe using as.dataframe and converting the column using as.factor.
I have dataframes a,b,c and list
b$cat2 <-list[match (b$` Description`, list$Var1),"cat2",]
b2 <-b[,c("Number", "DOB", "Gender", "cat2"),]
colnames (b2) <- c ("Number", "DOB","Gender", "group" )
b2 <- as.data.frame (b2)
a$group <- as.factor (a$group)
class (b2$group )
[1] "tbl_df" "tbl" "data.frame"
b2$group <- as.factor (b2$group)
Error: Can't use matrix or array for column indexing
Call `rlang::last_error()` to see a backtrace
#combine a and b2
combo <- rbind (a, b2)
Warning message:
In `[<-.factor`(`*tmp*`, ri, value = list(cat2 = c(1, 0, 1, 1, 0, :
invalid factor level, NA generated
r dataframe
r dataframe
edited Jan 21 at 23:23
sar
asked Dec 31 '18 at 16:41
sarsar
639
639
closed as off-topic by phiver, PoGibas, Rui Barradas, MLavoie, Billal Begueradj Jan 1 at 3:16
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – phiver, PoGibas, Rui Barradas, MLavoie, Billal Begueradj
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by phiver, PoGibas, Rui Barradas, MLavoie, Billal Begueradj Jan 1 at 3:16
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – phiver, PoGibas, Rui Barradas, MLavoie, Billal Begueradj
If this question can be reworded to fit the rules in the help center, please edit the question.
3
The issue would be that thegroup
column in 'b2' is itself a data.frame/tbl_df.. If it is a single column dataframe, try ``b2$group <-factor( unlist(b2$group))
– akrun
Dec 31 '18 at 16:42
Fantastic! Appreciate your help so much!
– sar
Dec 31 '18 at 16:56
If you could edit this into a much simpler example that only illustrates the problem that would be very helpful.
– Elin
Dec 31 '18 at 17:46
add a comment |
3
The issue would be that thegroup
column in 'b2' is itself a data.frame/tbl_df.. If it is a single column dataframe, try ``b2$group <-factor( unlist(b2$group))
– akrun
Dec 31 '18 at 16:42
Fantastic! Appreciate your help so much!
– sar
Dec 31 '18 at 16:56
If you could edit this into a much simpler example that only illustrates the problem that would be very helpful.
– Elin
Dec 31 '18 at 17:46
3
3
The issue would be that the
group
column in 'b2' is itself a data.frame/tbl_df.. If it is a single column dataframe, try ``b2$group <-factor( unlist(b2$group))
– akrun
Dec 31 '18 at 16:42
The issue would be that the
group
column in 'b2' is itself a data.frame/tbl_df.. If it is a single column dataframe, try ``b2$group <-factor( unlist(b2$group))
– akrun
Dec 31 '18 at 16:42
Fantastic! Appreciate your help so much!
– sar
Dec 31 '18 at 16:56
Fantastic! Appreciate your help so much!
– sar
Dec 31 '18 at 16:56
If you could edit this into a much simpler example that only illustrates the problem that would be very helpful.
– Elin
Dec 31 '18 at 17:46
If you could edit this into a much simpler example that only illustrates the problem that would be very helpful.
– Elin
Dec 31 '18 at 17:46
add a comment |
1 Answer
1
active
oldest
votes
Based on the class
, the column itself is a tbl_df/data.frame
. According to factor
the input argument 'x' is a vector
x - a vector of data, usually taking a small number of distinct
values.
Assuming that it is a single column data.frame, we can unlist
and convert it to factor
b2$group <- factor( unlist(b2$group))
Or use extraction with [[
b2$group <- factor(b2$group[[1]])
1
Alternatively could use pull().
– Elin
Dec 31 '18 at 17:45
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Based on the class
, the column itself is a tbl_df/data.frame
. According to factor
the input argument 'x' is a vector
x - a vector of data, usually taking a small number of distinct
values.
Assuming that it is a single column data.frame, we can unlist
and convert it to factor
b2$group <- factor( unlist(b2$group))
Or use extraction with [[
b2$group <- factor(b2$group[[1]])
1
Alternatively could use pull().
– Elin
Dec 31 '18 at 17:45
add a comment |
Based on the class
, the column itself is a tbl_df/data.frame
. According to factor
the input argument 'x' is a vector
x - a vector of data, usually taking a small number of distinct
values.
Assuming that it is a single column data.frame, we can unlist
and convert it to factor
b2$group <- factor( unlist(b2$group))
Or use extraction with [[
b2$group <- factor(b2$group[[1]])
1
Alternatively could use pull().
– Elin
Dec 31 '18 at 17:45
add a comment |
Based on the class
, the column itself is a tbl_df/data.frame
. According to factor
the input argument 'x' is a vector
x - a vector of data, usually taking a small number of distinct
values.
Assuming that it is a single column data.frame, we can unlist
and convert it to factor
b2$group <- factor( unlist(b2$group))
Or use extraction with [[
b2$group <- factor(b2$group[[1]])
Based on the class
, the column itself is a tbl_df/data.frame
. According to factor
the input argument 'x' is a vector
x - a vector of data, usually taking a small number of distinct
values.
Assuming that it is a single column data.frame, we can unlist
and convert it to factor
b2$group <- factor( unlist(b2$group))
Or use extraction with [[
b2$group <- factor(b2$group[[1]])
answered Dec 31 '18 at 17:41
akrunakrun
407k13198273
407k13198273
1
Alternatively could use pull().
– Elin
Dec 31 '18 at 17:45
add a comment |
1
Alternatively could use pull().
– Elin
Dec 31 '18 at 17:45
1
1
Alternatively could use pull().
– Elin
Dec 31 '18 at 17:45
Alternatively could use pull().
– Elin
Dec 31 '18 at 17:45
add a comment |
3
The issue would be that the
group
column in 'b2' is itself a data.frame/tbl_df.. If it is a single column dataframe, try ``b2$group <-factor( unlist(b2$group))
– akrun
Dec 31 '18 at 16:42
Fantastic! Appreciate your help so much!
– sar
Dec 31 '18 at 16:56
If you could edit this into a much simpler example that only illustrates the problem that would be very helpful.
– Elin
Dec 31 '18 at 17:46