Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert from list to numeric

I am tying to coerce from list form to numeric form. If it is of use, the list is originally drawn from a factor (and is 1x33 rows).

My list is defined by:

tmpseqsf[[1]]

which provides:

       TradeValue
1    72914431
2       25325
3       20139
4       ...

So based on other advice (Stackoverflow, etc) I use:

tmpx <-as.numeric(tmpseqsf[[1]])

but I get the error:

Error: (list) object cannot be coerced to type 'double'

And just confirming nothing has happened:

is.numeric(tmpseqsf[[1]])
[1] FALSE

Is there something I am completely missing?

like image 511
Tim Avatar asked Apr 24 '17 23:04

Tim


People also ask

How do I convert a list to a number in Python?

The most Pythonic way to convert a list of strings to a list of ints is to use the list comprehension [int(x) for x in strings] . It iterates over all elements in the list and converts each list element x to an integer value using the int(x) built-in function.

How do I convert a list to a numeric DataFrame in R?

Convert List to DataFrame using data. data. frame() is used to create a DataFrame in R that takes a list, vector, array, etc as arguments, Hence, we can pass a created list to the data. frame() function to convert list to DataFrame. It will store the elements in a single row in the DataFrame.

How do I convert a list to characters in R?

Using toString() to Convert List to String. The toString() function can be used to convert elements from list to character string, but this function by default adds a comma separator between the elements.

How do I turn a list into an integer list?

To convert a list of strings to a list of integers: Pass the int() class and the list to the map() function. The map() function will pass each item of the list to the int() class. The new list will only contain integer values.


1 Answers

If the values are of type factor, then you should convert them by as.numeric(as.character(unlist(tmpseqsf[[1]])))

like image 117
ahly Avatar answered Oct 18 '22 03:10

ahly