Converting Characters to Numeric for Data Set

I've downloaded RStudio on a new computer and the newer version is prohibiting me from changing character variables to numeric. All of the data is being converted to NAs and I've looked around on discussion boards and haven't had any luck.

May16 <- read.csv("mlbbatterMay16XGRPROJ.csv", header = TRUE)

May16$Position <- as.numeric(May16$Position)
Warning message:
NAs introduced by coercion
May16$First.Name <- as.numeric(May16$First.Name)
Warning message:
NAs introduced by coercion
May16$Nickname <- as.numeric(May16$Nickname)
Warning message:
NAs introduced by coercion
May16$Last.Name <- as.numeric(May16$Last.Name)
Warning message:
NAs introduced by coercion
May16$Team <- as.numeric(May16$Team)
Warning message:
NAs introduced by coercion
May16$Opponent <- as.numeric(May16$Opponent)
Warning message:
NAs introduced by coercion

View(May16)

Please post the output of

dput(head(May16))

Type a line containing three back ticks just before and just after the pasted output, like this:
```
your output here
```

Can First.Name be converted to numeric?

structure(list(Id = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1), Position = c("OF", 
"3B", "OF", "OF", "SS", "3B"), First.Name = c("Mitch", "Rafael", 
"Trent", "Alex", "Carlos", "Alex"), Nickname = c("Mitch Haniger", 
"Rafael Devers", "Trent Grisham", "Alex Verdugo", "Carlos Correa", 
"Alex Bregman"), Last.Name = c("Haniger", "Devers", "Grisham", 
"Verdugo", "Correa", "Bregman"), FPPG = c(12.53333367, 12.76470588, 
13.46363692, 11.6833335, 9.476470947, 9.636363636), Played = c(18L, 
17L, 11L, 18L, 17L, 11L), Salary = c(3500L, 3400L, 3600L, 3400L, 
3700L, 4200L), Game = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1), Team = c("SEA", 
"BOS", "SD", "BOS", "HOU", "HOU"), Opponent = c("BOS", "SEA", 
"LAD", "SEA", "LAA", "LAA"), Injury.Indicator = c(0.1, 0.1, 0.1, 
0.1, 0.1, 0.1), Injury.Details = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1
), Tier = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1), Probable.Pitcher = c(0.1, 
0.1, 0.1, 0.1, 0.1, 0.1), Batting.Order = c(0.1, 0.1, 0.1, 0.1, 
0.1, 0.1), Roster.Position = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1), 
    Line = c(142L, -168L, 144L, -168L, -130L, -130L), OU = c(9, 
    9, 8, 9, 8.5, 8.5), TMPts = c(3.65, 5.45, 3.15, 5.45, 5.05, 
    5.05), Swish = c(13.33, 13.22, 10.3, 11.94, 10.28, 10.19), 
    NFPts = c(14.5, 12.8, 10.3, 14.1, 9.7, 11.1), PtsAVG = c(13.915, 
    13.01, 10.3, 13.02, 9.99, 10.645), Today.s.Score = c(34.9, 
    30.9, 27.7, 24, 22.6, 22.5), PAProj = c(5, 5, 4, 5, 5, 5), 
    X1BProj = c(0.1, 1, 1, 1, 2, 3), X2BProj = c(0.1, 0.1, 0.1, 
    0.1, 0.1, 0.1), X3BProj = c(0.1, 0.1, 0.1, 1, 0.1, 0.1), 
    HRProj = c(1, 1, 1, 0.1, 0.1, 0.1), RProj = c(2, 2, 1, 0.1, 
    3, 0.1), RBIProj = c(3, 1, 1, 0.1, 2, 3), BBProj = c(2, 0.1, 
    0.1, 0.1, 0.1, 0.1), SBProj = c(0.1, 1, 1, 2, 0.1, 0.1), 
    Value = c(3.975714286, 3.826470588, 2.861111111, 3.829411765, 
    2.7, 2.53452381), NFPA = c(4.86, 4.3, 4.72, 4.64, 4.26, 4.37
    ), NFBB = c(0.4, 0.4, 0.48, 0.38, 0.2, 0.51), NF1B = c(0.54, 
    0.69, 0.63, 0.81, 0.67, 0.77), NF2B = c(0.26, 0.24, 0.19, 
    0.27, 0.26, 0.22), NF3B = c(0.03, 0.01, 0.03, 0.02, 0.01, 
    0.1), NFHR = c(0.32, 0.25, 0.13, 0.18, 0.16, 0.15), NFR = c(0.74, 
    0.72, 0.52, 0.84, 0.46, 0.6), NFRBI = c(0.83, 0.69, 0.47, 
    0.72, 0.56, 0.57), NFSB = c(0.07, 0.03, 0.11, 0.19, 0.1, 
    0.02), PNHD = c("R", "R", "R", "R", "R", "R"), HandFP = c(10.31176471, 
    9.2125, 11.98888889, 10.63333333, 5.294117647, 3.09), HandBB = c(0.03389831, 
    0.10204082, 0.17142857, 0.09433962, 0.05263158, 0.04347826
    ), HandK = c(0.20338983, 0.24489796, 0.2, 0.13207547, 0.18421053, 
    0.13043478), HandBB.K = c(0.16666667, 0.41666667, 0.85714286, 
    0.71428571, 0.28571429, 0.33333333), HandAVG = c(0.33928571, 
    0.25581395, 0.27586207, 0.34090909, 0.22222222, 0.22727273
    ), HandOBP = c(0.3559322, 0.32653061, 0.4, 0.41509434, 0.28205128, 
    0.26086957), HandSLG = c(0.60714286, 0.58139535, 0.51724138, 
    0.56818182, 0.47222222, 0.27272727), HandOPS = c(0.96307506, 
    0.90792596, 0.91724138, 0.98327616, 0.7542735, 0.53359684
    ), HandISO = c(0.26785715, 0.3255814, 0.24137931, 0.22727273, 
    0.25, 0.04545454), HandBABIP = c(0.38095238, 0.25, 0.3, 0.35135135, 
    0.22222222, 0.26315789), HandwRC = c(11.45707655, 8.271634786, 
    6.598861531, 10.48686131, 4.915898671, 1.396395987), HandwRAA = c(4.56474885, 
    2.547498225, 2.510192559, 4.295448297, 0.476772358, -1.290443624
    ), HandwOBA = c(0.406917802, 0.375080826, 0.399831516, 0.411531196, 
    0.325600693, 0.239477839), HandwRC. = c(170.456058, 140.546033, 
    153.8241427, 165.4891316, 115.1083681, 56.17423811), wOBATotal = c(0.402305934, 
    0.341360275, 0.373943839, 0.378510399, 0.313866898, 0.297492613
    ), ISOTotal = c(0.282952383, 0.2151938, 0.163793103, 0.20709091, 
    0.167666667, 0.07081818), FPG = c(12.53333333, 12.76470588, 
    13.46363636, 11.68333333, 9.476470588, 9.636363636), BB. = c(0.038, 
    0.11, 0.149, 0.067, 0.069, 0.087), K. = c(0.231, 0.233, 0.213, 
    0.16, 0.167, 0.174), BB.K = c(0.17, 0.47, 0.7, 0.42, 0.42, 
    0.5), AVG = c(0.315, 0.27, 0.3, 0.288, 0.273, 0.286), OBP = c(0.333, 
    0.356, 0.404, 0.347, 0.333, 0.348), SLG = c(0.575, 0.54, 
    0.55, 0.5, 0.47, 0.452), OPS = c(0.909, 0.896, 0.954, 0.847, 
    0.803, 0.8), ISO = c(0.26, 0.27, 0.25, 0.212, 0.197, 0.167
    ), Spd = c(4.3, 1.8, 5.4, 2.6, 1.1, 0.9), BABIP = c(0.358, 
    0.286, 0.333, 0.302, 0.294, 0.313), UBR = c(-0.3, 0.2, 0.5, 
    0.2, -0.2, -0.3), wSB = c(-0.1, -0.5, 0.5, -0.1, -0.1, -0.1
    ), wRC = c(14, 13, 9, 12, 11, 7), wRAA = c(4.5, 4, 3.9, 2.9, 
    2.3, 1.2), wOBA = c(0.382, 0.379, 0.413, 0.358, 0.35, 0.344
    ), wRC. = c(154, 143, 162, 129, 132, 127), L7PA = c(29L, 
    24L, 24L, 25L, 20L, 8L), L71B = c(5, 4, 6, 5, 3, 2), L72B = c(1, 
    1, 0.1, 1, 1, 0.1), L73B = c(1, 0.1, 0.1, 0.1, 0.1, 0.1), 
    L7HR = c(2, 0.1, 0.1, 1, 0.1, 0.1), L7R = c(5, 2, 2, 3, 4, 
    1), L7RBI = c(7, 3, 1, 5, 1, 0.1), L7BB = c(1, 3, 3, 2, 2, 
    1), L7SB = c(0.1, 0.1, 3, 0.1, 0.1, 0.1), L7PtsG = c(13.92857143, 
    7.316666667, 9.15, 9.442857143, 7.46, 6.1), L7PtsPA = c(3.362068966, 
    1.829166667, 2.2875, 2.644, 1.865, 1.525), L7BB. = c(0.034, 
    0.125, 0.125, 0.08, 0.1, 0.125), L7OBP = c(0.345, 0.333, 
    0.375, 0.36, 0.3, 0.375), L7SLG = c(0.643, 0.3, 0.286, 0.5, 
    0.278, 0.286), L7OPS = c(0.988, 0.633, 0.661, 0.86, 0.578, 
    0.661), L7ISO = c(0.321, 0.05, 0.1, 0.182, 0.056, 0.1), L7BABIP = c(0.35, 
    0.333, 0.375, 0.375, 0.25, 0.333), L7wSB = c(0.1, 0.1, 0.6, 
    0.1, 0.1, 0.1), L7wRC = c(6, 2, 3, 4, 2, 1), L7wRAA = c(2.5, 
    -0.8, 0.1, 1.1, -0.7, 0.1), L7wOBA = c(0.418, 0.27, 0.309, 
    0.366, 0.266, 0.309), L7wRC. = c(178, 68, 96, 134, 74, 103
    ), Pproj = c(24.34, 19.2, 35.03, 19.2, 23.66, 23.66), FPGS = c(26.2, 
    24.3, 28, 24.3, 36.3, 36.3), FPHA = c(24.82243902, 25.98, 
    30.8, 25.98, 36.88153846, 36.88153846), PL30 = c(26.2, 24.3, 
    28, 24.3, 36.3, 36.3), XBH = c(18, 18, 18, 9, 0.1, 0.1), 
    XBHValue = c(5.142857143, 5.294117647, 5, 2.647058824, 0.1, 
    0.1), L7PAProj = c(16.81034483, 9.145833333, 9.15, 13.22, 
    9.325, 7.625), FPPA = c(2.892307692, 2.97260274, 3.15106383, 
    2.804, 2.2375, 2.304347826), PAProj.1 = c(14.46153846, 14.8630137, 
    12.60425532, 14.02, 11.1875, 11.52173913), TBPA = c(0.538461538, 
    0.465753425, 0.468085106, 0.44, 0.430555556, 0.413043478), 
    TBProj = c(2.692307692, 2.328767123, 1.872340426, 2.2, 2.152777778, 
    2.065217391), HandFPPA = c(2.971186441, 3.008163265, 3.082857143, 
    3.009433962, 2.368421053, 1.343478261), HandPAProj = c(14.8559322, 
    15.04081633, 12.33142857, 15.04716981, 11.84210526, 6.717391304
    )), row.names = c(NA, 6L), class = "data.frame")```

And just to clarify, I've done this successfully for a long time on an earlier version of RStudio, so I'm not sure why an updated version would not work.

I now remember that in R versions before 4.0.0, read.csv() had a default of stringsAsFactors = TRUE. In more recent version, the default is FALSE. Try

May16 <- read.csv("mlbbatterMay16XGRPROJ.csv", header = TRUE,
                   stringsAsFactors = TRUE)

Brilliant. That works! Thank you!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.