Changing variable values in dataframe

I took the " " away and am still running into an error.

CODE:

fdecteamstats=filter(decteamstats, Team == "Seattle U") %>%
  mutate("NETRTG" = ifelse(NETRTG <= 0, 0, 1)) %>%
  mutate_if(is.numeric, round) %>%
  rename("W/L" = NETRTG) %>%
  mutate("Home" = ifelse(Home == "Seattle U", 1, 0)) %>%
  rowwise() %>%
  mutate("ID" = (setdiff(c(Home,Away), Team)))

ERROR:

Error: Problem with `mutate()` input `ID`.
x Input `ID` can't be recycled to size 1.
ℹ Input `ID` is `(setdiff(c(Home, Away), Team))`.
ℹ Input `ID` must be size 1, not 2.
ℹ Did you mean: `ID = list((setdiff(c(Home, Away), Team)))` ?
ℹ The error occurred in row 4.

Please provide the result of

dput(decteamstats)

Sorry the output is a lot. Also do you have any good beginner learning resources? I feel bad asking these type of questions to experts

> dput(decteamstats)
structure(list(ID = c(4983535, 4983535, 4984328, 4984328, 4984564, 
4984564, 4985254, 4985254, 4985790, 4985790, 4986583, 4986583, 
4986962, 4986962, 4988878, 4988878, 4989835, 4989835, 4996158, 
4996158, 4996988, 4996988), Home = c("UCLA", "UCLA", "Long Beach St.", 
"Long Beach St.", "Washington", "Washington", "Seattle U", "Seattle U", 
"Seattle U", "Seattle U", "California", "California", "Seattle U", 
"Seattle U", "Seattle U", "Seattle U", "Seattle U", "Seattle U", 
"Dixie St.", "Dixie St.", "Dixie St.", "Dixie St."), Away = c("Seattle U", 
"Seattle U", "Seattle U", "Seattle U", "Seattle U", "Seattle U", 
"Northwest (WA)", "Northwest (WA)", "Col. of Idaho", "Col. of Idaho", 
"Seattle U", "Seattle U", "Portland", "Portland", "Saint Martin's", 
"Saint Martin's", "Utah Valley", "Utah Valley", "Seattle U", 
"Seattle U", "Seattle U", "Seattle U"), Team = c("Seattle U", 
"UCLA", "Long Beach St.", "Seattle U", "Seattle U", "Washington", 
"Northwest (WA)", "Seattle U", "Col. of Idaho", "Seattle U", 
"California", "Seattle U", "Portland", "Seattle U", "Saint Martin's", 
"Seattle U", "Seattle U", "Utah Valley", "Dixie St.", "Seattle U", 
"Dixie St.", "Seattle U"), Mins = c(38.683, 38.683, 38.167, 38.167, 
38.683, 38.683, 39.183, 39.183, 39.183, 39.183, 39.083, 39.083, 
39.917, 39.917, 38.817, 38.817, 43.583, 43.583, 39.767, 39.767, 
39.483, 39.483), oMins = c(19.6, 19.083, 16.533, 21.633, 21.083, 
17.6, 22.767, 16.417, 20.117, 19.067, 19.217, 19.867, 19.067, 
20.85, 20.817, 18, 22.717, 20.867, 19, 20.767, 17.85, 21.633), 
    dMins = c(19.083, 19.6, 21.633, 16.533, 17.6, 21.083, 16.417, 
    22.767, 19.067, 20.117, 19.867, 19.217, 20.85, 19.067, 18, 
    20.817, 20.867, 22.717, 20.767, 19, 21.633, 17.85), oPOSS = c(72, 
    72, 77, 76, 68, 69, 69, 70, 70, 68, 64, 63, 76, 79, 73, 74, 
    92, 90, 73, 75, 76, 78), dPOSS = c(72, 72, 76, 77, 69, 68, 
    70, 69, 68, 70, 63, 64, 79, 76, 74, 73, 90, 92, 75, 73, 78, 
    76), ORTG = c(72.222, 108.333, 103.896, 98.684, 60.294, 105.797, 
    57.971, 127.143, 77.143, 114.706, 109.375, 103.175, 89.474, 
    106.329, 86.301, 132.432, 100, 103.333, 105.479, 101.333, 
    73.684, 98.718), DRTG = c(108.333, 72.222, 98.684, 103.896, 
    105.797, 60.294, 127.143, 57.971, 114.706, 77.143, 103.175, 
    109.375, 106.329, 89.474, 132.432, 86.301, 103.333, 100, 
    101.333, 105.479, 98.718, 73.684), NETRTG = c(-36.111, 36.111, 
    5.212, -5.212, -45.503, 45.503, -69.172, 69.172, -37.563, 
    37.563, 6.2, -6.2, -16.855, 16.855, -46.131, 46.131, -3.333, 
    3.333, 4.146, -4.146, -25.034, 25.034), PTS = c(52, 78, 80, 
    75, 41, 73, 40, 89, 54, 78, 70, 65, 68, 84, 63, 98, 92, 93, 
    77, 76, 56, 77), dPTS = c(78, 52, 75, 80, 73, 41, 89, 40, 
    78, 54, 65, 70, 84, 68, 98, 63, 93, 92, 76, 77, 77, 56), 
    FGA = c(59, 51, 59, 64, 59, 56, 58, 59, 53, 63, 53, 60, 64, 
    68, 69, 60, 73, 57, 64, 71, 57, 57), dFGA = c(51, 59, 64, 
    59, 56, 59, 59, 58, 63, 53, 60, 53, 68, 64, 60, 69, 57, 73, 
    71, 64, 57, 57), FGM = c(23, 28, 28, 24, 17, 26, 14, 34, 
    18, 28, 25, 27, 22, 33, 23, 33, 25, 30, 22, 27, 19, 24), 
    dFGM = c(28, 23, 24, 28, 26, 17, 34, 14, 28, 18, 27, 25, 
    33, 22, 33, 23, 30, 25, 27, 22, 24, 19), TPA = c(23, 16, 
    16, 33, 23, 15, 27, 21, 19, 26, 19, 23, 22, 23, 20, 28, 24, 
    14, 19, 34, 18, 27), dTPA = c(16, 23, 33, 16, 15, 23, 21, 
    27, 26, 19, 23, 19, 23, 22, 28, 20, 14, 24, 34, 19, 27, 18
    ), TPM = c(3, 6, 5, 13, 4, 8, 5, 5, 6, 5, 6, 5, 7, 8, 7, 
    13, 9, 4, 5, 11, 4, 10), dTPM = c(6, 3, 13, 5, 8, 4, 5, 5, 
    5, 6, 5, 6, 8, 7, 13, 7, 4, 9, 11, 5, 10, 4), FTA = c(9, 
    24, 26, 23, 7, 15, 7, 20, 20, 20, 21, 6, 23, 16, 18, 25, 
    40, 41, 31, 15, 20, 27), dFTA = c(24, 9, 23, 26, 15, 7, 20, 
    7, 20, 20, 6, 21, 16, 23, 25, 18, 41, 40, 15, 31, 27, 20), 
    FTM = c(3, 16, 19, 14, 3, 13, 7, 16, 12, 17, 14, 6, 17, 10, 
    10, 19, 33, 29, 28, 11, 14, 19), dFTM = c(16, 3, 14, 19, 
    13, 3, 16, 7, 17, 12, 6, 14, 10, 17, 19, 10, 29, 33, 11, 
    28, 19, 14), RIMA = c(22, 28, 20, 13, 9, 15, 14, 24, 14, 
    22, 21, 27, 28, 32, 20, 21, 24, 23, 27, 25, 28, 19), dRIMA = c(28, 
    22, 13, 20, 15, 9, 24, 14, 22, 14, 27, 21, 32, 28, 21, 20, 
    23, 24, 25, 27, 19, 28), RIMM = c(14, 19, 16, 8, 6, 12, 5, 
    17, 8, 15, 13, 18, 12, 22, 9, 14, 9, 15, 11, 12, 12, 10), 
    dRIMM = c(19, 14, 8, 16, 12, 6, 17, 5, 15, 8, 18, 13, 22, 
    12, 14, 9, 15, 9, 12, 11, 10, 12), ORB = c(6, 7, 9, 11, 8, 
    9, 14, 6, 13, 13, 9, 9, 8, 11, 14, 8, 13, 9, 15, 15, 14, 
    9), dORB = c(7, 6, 11, 9, 9, 8, 6, 14, 13, 13, 9, 9, 11, 
    8, 8, 14, 9, 13, 15, 15, 9, 14), DRB = c(18, 35, 34, 25, 
    21, 33, 20, 28, 24, 24, 23, 23, 26, 36, 22, 35, 20, 36, 32, 
    29, 28, 29), dDRB = c(35, 18, 25, 34, 33, 21, 28, 20, 24, 
    24, 23, 23, 36, 26, 35, 22, 36, 20, 29, 32, 29, 28), BLK = c(3, 
    4, 2, 2, 1, 5, 1, 7, 3, 6, 2, 1, 5, 8, 4, 5, 3, 5, 2, 5, 
    2, 3), dBLK = c(4, 3, 2, 2, 5, 1, 7, 1, 6, 3, 1, 2, 8, 5, 
    5, 4, 5, 3, 5, 2, 3, 2), TO = c(14, 15, 14, 11, 14, 16, 23, 
    9, 18, 9, 10, 8, 10, 13, 10, 8, 11, 22, 10, 12, 23, 17), 
    dTO = c(15, 14, 11, 14, 16, 14, 9, 23, 9, 18, 8, 10, 13, 
    10, 8, 10, 22, 11, 12, 10, 17, 23), AST = c(9, 15, 15, 12, 
    10, 11, 8, 12, 9, 13, 13, 14, 9, 22, 9, 21, 5, 9, 12, 11, 
    9, 8), dAST = c(15, 9, 12, 15, 11, 10, 12, 8, 13, 9, 14, 
    13, 22, 9, 21, 9, 9, 5, 11, 12, 8, 9), ePOSS = c(70.5, 70.5, 
    75.5, 75.5, 69, 69, 71, 71, 68, 68, 63, 63, 77.5, 77.5, 73, 
    73, 89.5, 89.5, 74.5, 74.5, 77, 77), FG. = c(0.39, 0.549, 
    0.475, 0.375, 0.288, 0.464, 0.241, 0.576, 0.34, 0.444, 0.472, 
    0.45, 0.344, 0.485, 0.333, 0.55, 0.342, 0.526, 0.344, 0.38, 
    0.333, 0.421), dFG. = c(0.549, 0.39, 0.375, 0.475, 0.464, 
    0.288, 0.576, 0.241, 0.444, 0.34, 0.45, 0.472, 0.485, 0.344, 
    0.55, 0.333, 0.526, 0.342, 0.38, 0.344, 0.421, 0.333), TPP = c(0.13, 
    0.375, 0.312, 0.394, 0.174, 0.533, 0.185, 0.238, 0.316, 0.192, 
    0.316, 0.217, 0.318, 0.348, 0.35, 0.464, 0.375, 0.286, 0.263, 
    0.324, 0.222, 0.37), dTPP = c(0.375, 0.13, 0.394, 0.312, 
    0.533, 0.174, 0.238, 0.185, 0.192, 0.316, 0.217, 0.316, 0.348, 
    0.318, 0.464, 0.35, 0.286, 0.375, 0.324, 0.263, 0.37, 0.222
    ), FTP = c(0.333, 0.667, 0.731, 0.609, 0.429, 0.867, 1, 0.8, 
    0.6, 0.85, 0.667, 1, 0.739, 0.625, 0.556, 0.76, 0.825, 0.707, 
    0.903, 0.733, 0.7, 0.704), dFTP = c(0.667, 0.333, 0.609, 
    0.731, 0.867, 0.429, 0.8, 1, 0.85, 0.6, 1, 0.667, 0.625, 
    0.739, 0.76, 0.556, 0.707, 0.825, 0.733, 0.903, 0.704, 0.7
    ), eFG. = c(0.415, 0.608, 0.517, 0.477, 0.322, 0.536, 0.284, 
    0.619, 0.396, 0.484, 0.528, 0.492, 0.398, 0.544, 0.384, 0.658, 
    0.404, 0.561, 0.383, 0.458, 0.368, 0.509), deFG. = c(0.608, 
    0.415, 0.477, 0.517, 0.536, 0.322, 0.619, 0.284, 0.484, 0.396, 
    0.492, 0.528, 0.544, 0.398, 0.658, 0.384, 0.561, 0.404, 0.458, 
    0.383, 0.509, 0.368), TS. = c(0.411, 0.625, 0.561, 0.501, 
    0.329, 0.578, 0.326, 0.65, 0.432, 0.538, 0.556, 0.517, 0.454, 
    0.556, 0.406, 0.682, 0.5, 0.608, 0.489, 0.486, 0.421, 0.551
    ), dTS. = c(0.625, 0.411, 0.501, 0.561, 0.578, 0.329, 0.65, 
    0.326, 0.538, 0.432, 0.517, 0.556, 0.556, 0.454, 0.682, 0.406, 
    0.608, 0.5, 0.486, 0.489, 0.551, 0.421), RIM. = c(0.636, 
    0.679, 0.8, 0.615, 0.667, 0.8, 0.357, 0.708, 0.571, 0.682, 
    0.619, 0.667, 0.429, 0.688, 0.45, 0.667, 0.375, 0.652, 0.407, 
    0.48, 0.429, 0.526), dRIM. = c(0.679, 0.636, 0.615, 0.8, 
    0.8, 0.667, 0.708, 0.357, 0.682, 0.571, 0.667, 0.619, 0.688, 
    0.429, 0.667, 0.45, 0.652, 0.375, 0.48, 0.407, 0.526, 0.429
    ), MID. = c(0.429, 0.429, 0.304, 0.167, 0.259, 0.231, 0.235, 
    0.857, 0.2, 0.533, 0.462, 0.4, 0.214, 0.231, 0.241, 0.545, 
    0.28, 0.55, 0.333, 0.333, 0.273, 0.364), dMID. = c(0.429, 
    0.429, 0.167, 0.304, 0.231, 0.259, 0.857, 0.235, 0.533, 0.2, 
    0.4, 0.462, 0.231, 0.214, 0.545, 0.241, 0.55, 0.28, 0.333, 
    0.333, 0.364, 0.273), TPrate = c(0.39, 0.314, 0.271, 0.516, 
    0.39, 0.268, 0.466, 0.356, 0.358, 0.413, 0.358, 0.383, 0.344, 
    0.338, 0.29, 0.467, 0.329, 0.246, 0.297, 0.479, 0.316, 0.474
    ), dTPrate = c(0.314, 0.39, 0.516, 0.271, 0.268, 0.39, 0.356, 
    0.466, 0.413, 0.358, 0.383, 0.358, 0.338, 0.344, 0.467, 0.29, 
    0.246, 0.329, 0.479, 0.297, 0.474, 0.316), RIMrate = c(0.373, 
    0.549, 0.339, 0.203, 0.153, 0.268, 0.241, 0.407, 0.264, 0.349, 
    0.396, 0.45, 0.438, 0.471, 0.29, 0.35, 0.329, 0.404, 0.422, 
    0.352, 0.491, 0.333), dRIMrate = c(0.549, 0.373, 0.203, 0.339, 
    0.268, 0.153, 0.407, 0.241, 0.349, 0.264, 0.45, 0.396, 0.471, 
    0.438, 0.35, 0.29, 0.404, 0.329, 0.352, 0.422, 0.333, 0.491
    ), MIDrate = c(0.237, 0.137, 0.39, 0.281, 0.458, 0.464, 0.293, 
    0.237, 0.377, 0.238, 0.245, 0.167, 0.219, 0.191, 0.42, 0.183, 
    0.342, 0.351, 0.281, 0.169, 0.193, 0.193), dMIDrate = c(0.137, 
    0.237, 0.281, 0.39, 0.464, 0.458, 0.237, 0.293, 0.238, 0.377, 
    0.167, 0.245, 0.191, 0.219, 0.183, 0.42, 0.351, 0.342, 0.169, 
    0.281, 0.193, 0.193), FTrate = c(0.153, 0.471, 0.441, 0.359, 
    0.119, 0.268, 0.121, 0.339, 0.377, 0.317, 0.396, 0.1, 0.359, 
    0.235, 0.261, 0.417, 0.548, 0.719, 0.484, 0.211, 0.351, 0.474
    ), dFTrate = c(0.471, 0.153, 0.359, 0.441, 0.268, 0.119, 
    0.339, 0.121, 0.317, 0.377, 0.1, 0.396, 0.235, 0.359, 0.417, 
    0.261, 0.719, 0.548, 0.211, 0.484, 0.474, 0.351), ASTrate = c(0.391, 
    0.536, 0.536, 0.5, 0.588, 0.423, 0.571, 0.353, 0.5, 0.464, 
    0.52, 0.519, 0.409, 0.667, 0.391, 0.636, 0.2, 0.3, 0.545, 
    0.407, 0.474, 0.333), dASTrate = c(0.536, 0.391, 0.5, 0.536, 
    0.423, 0.588, 0.353, 0.571, 0.464, 0.5, 0.519, 0.52, 0.667, 
    0.409, 0.636, 0.391, 0.3, 0.2, 0.407, 0.545, 0.333, 0.474
    ), TOrate = c(0.194, 0.208, 0.182, 0.145, 0.206, 0.232, 0.333, 
    0.129, 0.257, 0.132, 0.156, 0.127, 0.132, 0.165, 0.137, 0.108, 
    0.12, 0.244, 0.137, 0.16, 0.303, 0.218), dTOrate = c(0.208, 
    0.194, 0.145, 0.182, 0.232, 0.206, 0.129, 0.333, 0.132, 0.257, 
    0.127, 0.156, 0.165, 0.132, 0.108, 0.137, 0.244, 0.12, 0.16, 
    0.137, 0.218, 0.303), BLKrate = c(0.059, 0.068, 0.031, 0.034, 
    0.018, 0.085, 0.017, 0.121, 0.048, 0.113, 0.033, 0.019, 0.074, 
    0.125, 0.067, 0.072, 0.053, 0.068, 0.028, 0.078, 0.035, 0.053
    ), oBLKrate = c(0.068, 0.059, 0.034, 0.031, 0.085, 0.018, 
    0.121, 0.017, 0.113, 0.048, 0.019, 0.033, 0.125, 0.074, 0.072, 
    0.067, 0.068, 0.053, 0.078, 0.028, 0.053, 0.035), ORB. = c(0.146, 
    0.28, 0.265, 0.244, 0.195, 0.3, 0.333, 0.231, 0.351, 0.351, 
    0.281, 0.281, 0.182, 0.297, 0.286, 0.267, 0.265, 0.31, 0.341, 
    0.319, 0.326, 0.243), DRB. = c(0.72, 0.854, 0.756, 0.735, 
    0.7, 0.805, 0.769, 0.667, 0.649, 0.649, 0.719, 0.719, 0.703, 
    0.818, 0.733, 0.714, 0.69, 0.735, 0.681, 0.659, 0.757, 0.674
    ), TimePerPoss = c(16.333, 15.903, 12.883, 17.079, 18.603, 
    15.304, 19.797, 14.071, 17.243, 16.824, 18.016, 18.921, 15.053, 
    15.835, 17.11, 14.595, 14.815, 13.911, 15.616, 16.613, 14.092, 
    16.641), dTimePerPoss = c(15.903, 16.333, 17.079, 12.883, 
    15.304, 18.603, 14.071, 19.797, 16.824, 17.243, 18.921, 18.016, 
    15.835, 15.053, 14.595, 17.11, 13.911, 14.815, 16.613, 15.616, 
    16.641, 14.092)), row.names = c(NA, -22L), groups = structure(list(
    ID = c(4983535, 4984328, 4984564, 4985254, 4985790, 4986583, 
    4986962, 4988878, 4989835, 4996158, 4996988), Home = c("UCLA", 
    "Long Beach St.", "Washington", "Seattle U", "Seattle U", 
    "California", "Seattle U", "Seattle U", "Seattle U", "Dixie St.", 
    "Dixie St."), Away = c("Seattle U", "Seattle U", "Seattle U", 
    "Northwest (WA)", "Col. of Idaho", "Seattle U", "Portland", 
    "Saint Martin's", "Utah Valley", "Seattle U", "Seattle U"
    ), .rows = structure(list(1:2, 3:4, 5:6, 7:8, 9:10, 11:12, 
        13:14, 15:16, 17:18, 19:20, 21:22), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, 11L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

Thanks for the catch. I'm going to have to define myself a == operator, such as eq, to fix this blind spot.

You can try this and see if it works.

Although I still don't think you know what the rename() function does. Try this:

decteamstats %>% 
  mutate(NETRTG = ifelse(NETRTG <= 0, 0, 1),
         Home   = ifelse(Home == "Seattle U", 1, 0),
         ID     = setdiff(c(Home, Away), Team)) %>% 
  rename(W/L = NETRTG) # I think you don't understand this function: rename() changes the name of a column, not a value in the column.

If you want to replace "W/L" with "NETRTG" then you can use your_column = ifelse(your_column == "W/L", "NETRTG", your_column) in the mutate function

library(tidyverse)
#I load the tidyverse since IDK which package has mutate
#this works for any number of conditions, much more readable than if
#At larger datasets mutate(var = case_when()) might be noticeably slow
#The TRUE ~ "Not a number" will catch any case that did not evaluate in your preceding conditions
df <- df%>%mutate(NETRTG = case_when(NETRTG < 0 ~ 0,
 NETRTG >= 0 ~ 1,
TRUE ~ "Not an number"
 ))

#I believe this should also work
#checks if NETRG is greater than or equal to zero
#NETRTG >=  0 returns a boolean, true or false
#wrapping it in as.integer converts the true to 1 and false to 0
df <- df%>%mutate(NETRTG = as.integer(NETRTG>=0 ))



the mutate of Home rather sabotages the attempt at mutating ID.
the idea of setdiff is that you get the difference between the set of home and away teams i.e. (Seattle , UCLA,) and compares the difference to the 'Team' i.e. if the Team is Seattle, the difference is UCLA, or if the Team is UCLA the difference is Seattle.
but your mutate of Home has turned it to 0 or 1 so the setdiff wont do what is wanted.

It was just in an order that didn't work. I switched some things around and now it is working well. Thank you guys. I would not have gotten it figured out without your guys help

This topic was automatically closed 7 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.