convert hyphen to zero in data frame then convert affected columns to numeric in R



I have data sets in which a hyphen is used in place of the number zero, as in the example data set my.data below. I can replace the hyphens with zeroes, but then have trouble converting the affected columns to numeric. My actual data sets are very large with many columns and I do not know which columns will contain hyphens. The data sets are also too large and complex for me to feel comfortable using Find and Replace within the data sets themselves before reading them into R.


I think the first three columns of the actual data sets will be character and the rest of the columns should be numeric (if it were not for the hyphens). Is there an efficient and


Related to : convert hyphen to zero in data frame then convert affected columns to numeric in R
Reshape data frame to convert factors into columns in R
Development Tools & Services

I have a data frame where one particular column has a set of specific values (let's say, 1, 2, ..., 23). What I would like to do is to convert from this layout to the one, where the frame would have extra 23 (in this case) columns, each one representing one of the factor values. The data in these columns would be booleans indicating whether a particular row had a given factor value... To show a specific example:


Source frame:


ID DATE SECTOR
123 2008-01-01 1
456 2008-01-01 3
789 2008-01-02 5
... <more records with SECTOR values from 1 to 5>

Desired forma

Convert data.frame columns from factors to characters
Development Tools & Services

I have a data frame. Let's call him bob:


> head(bob)
phenotype exclusion
GSM399350 3- 4- 8- 25- 44+ 11b- 11c- 19- NK1.1- Gr1- TER119-
GSM399351 3- 4- 8- 25- 44+ 11b- 11c- 19- NK1.1- Gr1- TER119-
GSM399352 3- 4- 8- 25- 44+ 11b- 11c- 19- NK1.1- Gr1- TER119-
GSM399353 3- 4- 8- 25+ 44+ 11b- 11c- 19- NK1.1- Gr1- TER119-
GSM399354 3- 4- 8- 25+ 44+ 11b- 11c- 19- NK1.1- Gr1- TER119-
GSM399355 3- 4- 8- 25+ 44+ 11b- 11c- 19- NK1.1- Gr1- TER119-

I'd like to concatenate the rows of this data frame (this will be another question). But look:


<
convert hyphen to zero in data frame then convert affected columns to numeric in R
Development Tools & Services

I have data sets in which a hyphen is used in place of the number zero, as in the example data set my.data below. I can replace the hyphens with zeroes, but then have trouble converting the affected columns to numeric. My actual data sets are very large with many columns and I do not know which columns will contain hyphens. The data sets are also too large and complex for me to feel comfortable using Find and Replace within the data sets themselves before reading them into R.


I think the first three columns of the actual data sets will be character and the rest of the columns should be numeric (if it were not for the hyphens). Is there an efficient and

Convert all data frame character columns to factors
Development Tools & Services
Given a (pre-existing) data frame that has columns of various types, what is the simplest way to convert all its character columns to factors, without affecting any columns of other types?
How to convert a data frame column to numeric type?
Development Tools & Services

How do you convert a data frame column to a numeric type?


How to convert data.frame column from Factor to numeric [duplicate]
Development Tools & Services

This question already has an answer here:

I have a data.frame whose class column is Factor. I'd like to convert it to numeric so that I can use correlation matrix.

> str(breast)
'data.frame':   699 obs. of  10 variables:
 ....
 $ class                   : Factor w/ 2 levels "2","4": 1 1 1 1 1 2 1
1 1 1 ...
> table(breast$class)
  2   4 
458 241
> cor(breast)
Error in cor(breast) : 'x' must be numeric

How can I convert a Factor column to a numeric column?



Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .