Hey Guys,
I wanted to creat a new column to filter wheter happend in Texas or a different state. This new variable is called "Istexas", but i get only negative results.
Hey Guys,
I wanted to creat a new column to filter wheter happend in Texas or a different state. This new variable is called "Istexas", but i get only negative results.
Hi, and welcome!
Unfortunately, screenshots are seldom useful. The the FAQ: What's a reproducible example (`reprex`) and how do I do one? for how to post code that can be worked with to help find solutions.
From what I can make out, there's probably an easier way to approach this. I'll used a ginned up data frame.
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(tibble))
dat <- structure(list(state = structure(c(3L, 4L, 4L, 32L, 21L, 47L,
37L, 25L, 22L, 14L, 38L, 26L, 35L, 37L, 39L, 6L, 5L, 15L, 32L,
5L, 19L, 43L, 1L, 8L, 32L, 49L, 41L, 6L, 37L, 34L, 43L, 15L,
16L, 19L, 24L, 25L, 8L, 49L, 29L, 22L, 18L, 24L, 36L, 28L, 37L,
16L, 26L, 28L, 30L, 43L, 9L, 18L, 25L, 13L, 45L, 12L, 3L, 3L,
44L, 32L, 24L, 40L, 8L, 20L, 13L, 28L, 36L, 35L, 45L, 6L, 21L,
31L, 13L, 14L, 47L, 21L, 48L, 30L, 43L, 6L, 2L, 35L, 49L, 9L,
7L, 26L, 23L, 19L, 2L, 8L, 42L, 2L, 9L, 14L, 25L, 24L, 49L, 49L,
36L, 28L, 7L, 23L, 3L, 21L, 31L, 10L, 4L, 36L, 11L, 49L, 4L,
34L, 19L, 31L, 12L, 32L, 36L, 25L, 43L, 21L, 38L, 6L, 21L, 19L,
5L, 5L, 15L, 33L, 1L, 26L, 34L, 6L, 5L, 37L, 14L, 37L, 47L, 25L,
10L, 40L, 4L, 15L, 4L, 21L, 30L, 27L, 49L, 43L, 11L, 15L, 46L,
8L, 48L, 48L, 3L, 36L, 9L, 32L, 18L, 4L, 15L, 45L, 33L, 23L,
49L, 8L, 35L, 30L, 38L, 48L, 47L, 42L, 48L, 5L, 14L, 37L, 42L,
37L, 24L, 2L, 32L, 12L, 27L, 35L, 25L, 15L, 14L, 47L, 45L, 30L,
48L, 15L, 9L, 48L, 17L, 41L, 31L, 24L, 8L, 21L), .Label = c("Alabama",
"Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut",
"Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois",
"Indiana", "Iowa", "Kentucky", "Louisiana", "Maine", "Maryland",
"Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri",
"Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey",
"New Mexico", "New York", "North Carolina", "North Dakota", "Ohio",
"Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina",
"South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia",
"Washington", "West Virginia", "Wisconsin", "Wyoming"), class = "factor"),
value = c(-0.0500677443851305, 0.20255093214272, -0.0382925606590059,
0.842050361189436, -0.798877956145816, -1.68232262518171,
0.728528650817833, 0.985837774682992, -1.70765697177937,
-0.160038596640867, 0.721607555842305, -0.4248095669316,
-0.0703716536202222, -0.515441373220822, -0.607659593956769,
0.590966244196858, 1.86541615611373, -0.624304007042872,
-0.260559170248893, 0.477814052255288, 1.27905182861303,
-0.96650997514641, -0.237572682646222, -0.337335716976861,
-2.46684945494161, 0.379837468880197, 0.0479904475705669,
0.563291104064344, 2.79454126780037, -0.466727682824244,
0.351165429744233, -0.259014297080849, 0.818619520037089,
-0.34885583004204, -0.880288189545205, -1.40547491665107,
-1.45708474221557, -0.271348203185227, -0.567222081707831,
0.491802786691456, 0.523967396376311, -0.193056996050934,
-0.224085896962664, 0.76573609838597, 1.13803008018725, 0.479745435828822,
1.45876122753945, -0.606953343622046, -0.740195778932835,
0.702650578965198, 0.903451986901155, -1.98448698066832,
-0.380139159867896, 1.43071408223918, 0.683328381180665,
-0.166913190856337, 0.0290560992653911, 0.811006738923476,
-0.366284863022354, -0.158016909103811, -2.11491634120928,
0.0800326394051908, 2.47630292748179, 0.62804240658866, 1.31059533789514,
1.58070018846522, -0.11322893024015, -0.433243197890605,
-0.433427603946357, -0.266666798889549, -0.233472482656319,
-1.60584401126863, -0.497343252452946, 1.07321951886125,
1.65431644426522, 0.924410502730337, -0.946163234285948,
0.744153019223842, 0.00721400339231964, 1.46910032266189,
0.300756875443599, 0.654827473013311, 0.447282537200565,
0.198008075524172, 0.00272492792992627, 0.993493754991008,
-0.385509051871126, -1.39073532351034, -1.50354727359961,
0.363942036412502, -0.933791292753043, 0.406834143748693,
-0.871559521243467, 0.797215614539181, 0.51753723350601,
0.732315117561597, 0.935782091055621, -0.0550377824545726,
1.77278859549965, -0.279975296434756, 0.512370288463287,
-0.128210402608692, -1.28007807753721, 0.666832961080878,
2.15033426091563, -0.240325127373937, 0.533136353514523,
-1.17385341594897, -1.24759543922249, -0.745768551818649,
0.248801955741311, 2.23608795312034, 0.0183359430283382,
0.440517156987265, -0.148848607579916, -0.432521774404884,
-1.66773631329721, 0.0675303269311713, -1.3297403826819,
0.338087598917726, -1.309815173351, 0.376571528646985, -1.34854528863469,
-0.123685528729531, -0.322133225839523, 0.84498816880702,
-0.873845088212342, -1.14074972669784, 0.960735294993471,
-0.487958264823631, 0.139626542824682, -0.749470294124344,
-0.997451660816324, 0.627275462152485, -0.836795958009802,
-0.845384452185865, 0.473223313355319, -0.186090958461863,
-0.891841975841899, 0.290483013894842, -1.54379348670249,
-1.11239148851224, 0.0217500342544884, 1.19832600460642,
0.98637741869489, 0.123466367191884, 0.597525075104177, 1.20983208255003,
0.496663997562984, 0.118392153581365, 0.535587807449442,
-1.80132661047085, -1.33625393567737, -0.693801148653181,
2.22266801758049, -1.14194171179068, 1.75957260962327, 0.284299765142467,
-0.916690887327047, 0.408561928862153, 0.837167055885323,
0.13663146471077, -1.7563743405609, 1.42341426043586, 0.169619995438837,
0.07903738540465, -0.618575685858797, 0.446159034278656,
-0.484526978038261, 0.321365505018176, 2.66194356635818,
2.01755421655406, 0.77523363771253, -0.95119697165064, 0.0811268241562798,
0.132201773876715, -0.698220813180683, -0.597477948270436,
0.977161860307835, -1.60480321495488, 0.50239474407725, -0.332805841859567,
0.540906667423175, 1.26119500623883, -0.795664250968672,
-1.20337244011018, -1.8190536943728, -0.723954827343716,
0.566651110548388, 0.0474640275314466, -0.541894247448819,
0.549433863347651, -1.30340209870227, 0.39105018129508, -0.72690419482481,
-1.3745366236515, -0.0189437262104834, -0.0974762970313,
-1.13939758440753, -4.2893577979456)), row.names = c(NA,
-200L), class = c("tbl_df", "tbl", "data.frame"))
dat %>% mutate(lone_star = ifelse(state == "Texas",1,0)) -> results
results %>% filter(lone_star == 1)
#> # A tibble: 3 x 3
#> state value lone_star
#> <fct> <dbl> <dbl>
#> 1 Texas -0.934 1
#> 2 Texas 2.02 1
#> 3 Texas -0.698 1
Created on 2020-02-13 by the reprex package (v0.3.0)
The errors are happening because of this line:
else {data[I, ]$Istexas <- FALSE}}data}
data
needs to go on a new line.
The first closing brace: FALSE
}
}data}
: this ends the else
statement.
The second closing brace: FALSE}
}
data}
: this ends the for
statement.
Now data
is showing up as an extra variable at the end of a finished statement, with no operator or extra code to say what it does. So R says it was an "unexpected symbol" and doesn't even try to run the code.
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.