a column(variable) in a data frame is also 'data.frame',how to change it into int or numeric?

0

Issue

‘str(data)’ to browse the data frame and found one variable is a ‘data.frame’, how can I change it into a normal vector/variable? Because now it cannot be exported correctly.

str(data) 
$ ID        : num [1:3177] 1.11e+11 1.11e+11 1.11e+11 1.11e+11 
$ indcate   :'data.frame':  3177 obs. of  1 variable:
  ..$ X: int [1:3177] 2 3 3 2 2 2 3 2 1 1 ...

(1)so I tried to change the type of this variable but failed

> as.numeric((data$indcate))
Error: 'list' object cannot be coerced to type 'double'
> as.integer((data$indcate))
Error: 'list' object cannot be coerced to type 'integer'
> as.factor((data$indcate))
 X 
<NA> 
Levels: c(2, 3, 1)
Warning message:
In xtfrm.data.frame(x) : cannot xtfrm data frames

(2)here is the export file, I export it as csv.file. There are some redundant columns behind.
enter image description here

Solution

Not sure I’ve totally understood but I think the issue is that your item is currently a list. You should be able to resolve it by adding unlist() to your code:

> as.numeric(unlist(indicfactor$indcate))

Answered By – anonymous2

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More