[R] Read in alphanumeric column without decimals

jim holtman jholtman at gmail.com
Thu Nov 29 22:33:50 CET 2012


Your problem is the the data read in from the spreadsheet is probably
a 'factor' since it has a non-numeric in the column.  To change it to
number you have to do the following

as.numeric(as.character(yourdata$TeamLeaderID))

What you as seeing with just the call to as.numeric is the value of
the 'factor', not the value of the number.

On Thu, Nov 29, 2012 at 2:53 PM, Steven Ranney <steven.ranney at gmail.com> wrote:
> All -
>
> How can I read in a column of alphanumeric values without including
> ".0" on the numeric values?
>
> Original column:
>
> TeamLeaderID
> 258
> 342
> 316
> U8
> 331
> 279
> D1
> 116
> 235
> 296
> ...
> [truncated]
>
> leaders = read.xlsx2('FILE', sheetIndex = 1, header = T)
>
> Column after it's been read in:
>
> leaders$TeamLeaderID
> 258.0
> 342.0
> 316.0
> U8
> 331.0
> 279.0
> D1
> 116.0
> 235.0
> 296.0
> ...
> [truncated]
>
> If I try
>
> as.numeric(leaders$TeamLeaderID), everything gets converted:
>
> leaders$TeamLeaderID
> 11
> 27
> 19
> 54
> 23
> 13
> 28
> 2
> 8
> 15
> ...
> [truncated]
>
> as.character() and as.vector() leave the ".0" in place.
>
> These data are being used to merge two files, one whose
> data$TeamLeaderID is identical to the original column of values above.
>  When I try to merge the dataframes by "TeamLeaderID," R can't match
> the numbers without decimal to the ones that have decimal points.  The
> alphanumeric TeamLeaderID values merge without a problem.  As a
> result, I get a dataframe filled with the proper values for those
> TeamLeaderID values that are alphanumeric, but "NA" values for those
> whose values are strictly numeric.
>
> How can I read in the values without R adding the ".0" to the end of
> the numerals?  If there isn't a way, how can I automate the removal of
> the ".0," as I have several 10s of TeamLeaderID values?
>
> I'm using 64-bit R v. 2.15.1 on a Windows 7 machine.
>
> Thank you -
>
> Steven H. Ranney
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.




More information about the R-help mailing list