What R object can hold variables so I can iterate over the object?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This seems simple, but I'm spinning my wheels.
I have 80 character variables. I want to 1) view the unique values in each variable, 2) view the number of unique values, and 3) convert some or all variables to factors.
I can do this one-by-one by explicitly naming the columns:
df$var1 # "val2" "val2" "val1" "val2" ...
unique(df$var1) # "val1" "val2" "val3"
length(unique(df$var1)) # 3
df$var1 = as.factor(df$var1)
... although this produces an error:
levels(df@var1)
# Error in levels(df@var1) :
# trying to get slot "var1" from an object (class "tbl_df")
# that is not an S4 object
But when I create a list of the character variables and try the same as above it doesn't work.
# create list of char vars
char_vars_names <- names(df[, sapply(df, class) == 'character'])
char_vars_names
#[1] "var1" "var2" "var3" ...
df$char_vars_names[1] # NULL
# Warning message: Unknown or uninitialised column: 'char_vars_names'.
unique(df$char_vars_names[1]) # NULL
length(unique(df$char_vars_names[1])) # 0
Once I get the above working I figure I can iterate over the list with a for loop:
for (i in range(length(char_vars_names))) {
# code
}
What is it about the char_vars_names object or elsewhere in my code that makes this not work?
In case it's helpful, here's my imports and data ingestion prior to the above code:
library(car)
library(lattice)
library(tidyverse)
library(ISLR)
library(Lahman)
library(mdsr)
file = "file.csv"
df <- read_csv(
file=file,
col_names = TRUE,
skip = 3,
na = c("", "NA"))
r for-loop variables
add a comment |
This seems simple, but I'm spinning my wheels.
I have 80 character variables. I want to 1) view the unique values in each variable, 2) view the number of unique values, and 3) convert some or all variables to factors.
I can do this one-by-one by explicitly naming the columns:
df$var1 # "val2" "val2" "val1" "val2" ...
unique(df$var1) # "val1" "val2" "val3"
length(unique(df$var1)) # 3
df$var1 = as.factor(df$var1)
... although this produces an error:
levels(df@var1)
# Error in levels(df@var1) :
# trying to get slot "var1" from an object (class "tbl_df")
# that is not an S4 object
But when I create a list of the character variables and try the same as above it doesn't work.
# create list of char vars
char_vars_names <- names(df[, sapply(df, class) == 'character'])
char_vars_names
#[1] "var1" "var2" "var3" ...
df$char_vars_names[1] # NULL
# Warning message: Unknown or uninitialised column: 'char_vars_names'.
unique(df$char_vars_names[1]) # NULL
length(unique(df$char_vars_names[1])) # 0
Once I get the above working I figure I can iterate over the list with a for loop:
for (i in range(length(char_vars_names))) {
# code
}
What is it about the char_vars_names object or elsewhere in my code that makes this not work?
In case it's helpful, here's my imports and data ingestion prior to the above code:
library(car)
library(lattice)
library(tidyverse)
library(ISLR)
library(Lahman)
library(mdsr)
file = "file.csv"
df <- read_csv(
file=file,
col_names = TRUE,
skip = 3,
na = c("", "NA"))
r for-loop variables
3
Trydf[[char_vars_names]][1]
. The$
notation can't take advantage of variable substitution, but the double brackets can.
– jdobres
Nov 23 '18 at 21:06
3
Also your use ofrange
will not work (I'm guessing you're coming from Python).for (i in char_vars_names)
might be what you're after.
– jdobres
Nov 23 '18 at 21:07
I get an error when I use the double bracket below, but maybe I didn't follow your suggestion correctly:char_vars_names # [1] "var1" "var2" ...
char_vars_names[1] # "var1"
# Works/works/doesn't work:df$var1 # "val2" "val2" "val1" ...
df[char_vars_names][1] # "val2" "val2" "val1" ...
df[[char_vars_names]][1] # Error in .subset2(x, i) : recursive indexing failed at level 2
– Karl Baker
Nov 24 '18 at 0:01
Thanks for the tip on the for loop. That is exactly what I'll need as soon as I can get the rest of the code to work!
– Karl Baker
Nov 24 '18 at 0:10
add a comment |
This seems simple, but I'm spinning my wheels.
I have 80 character variables. I want to 1) view the unique values in each variable, 2) view the number of unique values, and 3) convert some or all variables to factors.
I can do this one-by-one by explicitly naming the columns:
df$var1 # "val2" "val2" "val1" "val2" ...
unique(df$var1) # "val1" "val2" "val3"
length(unique(df$var1)) # 3
df$var1 = as.factor(df$var1)
... although this produces an error:
levels(df@var1)
# Error in levels(df@var1) :
# trying to get slot "var1" from an object (class "tbl_df")
# that is not an S4 object
But when I create a list of the character variables and try the same as above it doesn't work.
# create list of char vars
char_vars_names <- names(df[, sapply(df, class) == 'character'])
char_vars_names
#[1] "var1" "var2" "var3" ...
df$char_vars_names[1] # NULL
# Warning message: Unknown or uninitialised column: 'char_vars_names'.
unique(df$char_vars_names[1]) # NULL
length(unique(df$char_vars_names[1])) # 0
Once I get the above working I figure I can iterate over the list with a for loop:
for (i in range(length(char_vars_names))) {
# code
}
What is it about the char_vars_names object or elsewhere in my code that makes this not work?
In case it's helpful, here's my imports and data ingestion prior to the above code:
library(car)
library(lattice)
library(tidyverse)
library(ISLR)
library(Lahman)
library(mdsr)
file = "file.csv"
df <- read_csv(
file=file,
col_names = TRUE,
skip = 3,
na = c("", "NA"))
r for-loop variables
This seems simple, but I'm spinning my wheels.
I have 80 character variables. I want to 1) view the unique values in each variable, 2) view the number of unique values, and 3) convert some or all variables to factors.
I can do this one-by-one by explicitly naming the columns:
df$var1 # "val2" "val2" "val1" "val2" ...
unique(df$var1) # "val1" "val2" "val3"
length(unique(df$var1)) # 3
df$var1 = as.factor(df$var1)
... although this produces an error:
levels(df@var1)
# Error in levels(df@var1) :
# trying to get slot "var1" from an object (class "tbl_df")
# that is not an S4 object
But when I create a list of the character variables and try the same as above it doesn't work.
# create list of char vars
char_vars_names <- names(df[, sapply(df, class) == 'character'])
char_vars_names
#[1] "var1" "var2" "var3" ...
df$char_vars_names[1] # NULL
# Warning message: Unknown or uninitialised column: 'char_vars_names'.
unique(df$char_vars_names[1]) # NULL
length(unique(df$char_vars_names[1])) # 0
Once I get the above working I figure I can iterate over the list with a for loop:
for (i in range(length(char_vars_names))) {
# code
}
What is it about the char_vars_names object or elsewhere in my code that makes this not work?
In case it's helpful, here's my imports and data ingestion prior to the above code:
library(car)
library(lattice)
library(tidyverse)
library(ISLR)
library(Lahman)
library(mdsr)
file = "file.csv"
df <- read_csv(
file=file,
col_names = TRUE,
skip = 3,
na = c("", "NA"))
r for-loop variables
r for-loop variables
asked Nov 23 '18 at 20:54
Karl BakerKarl Baker
366115
366115
3
Trydf[[char_vars_names]][1]
. The$
notation can't take advantage of variable substitution, but the double brackets can.
– jdobres
Nov 23 '18 at 21:06
3
Also your use ofrange
will not work (I'm guessing you're coming from Python).for (i in char_vars_names)
might be what you're after.
– jdobres
Nov 23 '18 at 21:07
I get an error when I use the double bracket below, but maybe I didn't follow your suggestion correctly:char_vars_names # [1] "var1" "var2" ...
char_vars_names[1] # "var1"
# Works/works/doesn't work:df$var1 # "val2" "val2" "val1" ...
df[char_vars_names][1] # "val2" "val2" "val1" ...
df[[char_vars_names]][1] # Error in .subset2(x, i) : recursive indexing failed at level 2
– Karl Baker
Nov 24 '18 at 0:01
Thanks for the tip on the for loop. That is exactly what I'll need as soon as I can get the rest of the code to work!
– Karl Baker
Nov 24 '18 at 0:10
add a comment |
3
Trydf[[char_vars_names]][1]
. The$
notation can't take advantage of variable substitution, but the double brackets can.
– jdobres
Nov 23 '18 at 21:06
3
Also your use ofrange
will not work (I'm guessing you're coming from Python).for (i in char_vars_names)
might be what you're after.
– jdobres
Nov 23 '18 at 21:07
I get an error when I use the double bracket below, but maybe I didn't follow your suggestion correctly:char_vars_names # [1] "var1" "var2" ...
char_vars_names[1] # "var1"
# Works/works/doesn't work:df$var1 # "val2" "val2" "val1" ...
df[char_vars_names][1] # "val2" "val2" "val1" ...
df[[char_vars_names]][1] # Error in .subset2(x, i) : recursive indexing failed at level 2
– Karl Baker
Nov 24 '18 at 0:01
Thanks for the tip on the for loop. That is exactly what I'll need as soon as I can get the rest of the code to work!
– Karl Baker
Nov 24 '18 at 0:10
3
3
Try
df[[char_vars_names]][1]
. The $
notation can't take advantage of variable substitution, but the double brackets can.– jdobres
Nov 23 '18 at 21:06
Try
df[[char_vars_names]][1]
. The $
notation can't take advantage of variable substitution, but the double brackets can.– jdobres
Nov 23 '18 at 21:06
3
3
Also your use of
range
will not work (I'm guessing you're coming from Python). for (i in char_vars_names)
might be what you're after.– jdobres
Nov 23 '18 at 21:07
Also your use of
range
will not work (I'm guessing you're coming from Python). for (i in char_vars_names)
might be what you're after.– jdobres
Nov 23 '18 at 21:07
I get an error when I use the double bracket below, but maybe I didn't follow your suggestion correctly:
char_vars_names # [1] "var1" "var2" ...
char_vars_names[1] # "var1"
# Works/works/doesn't work: df$var1 # "val2" "val2" "val1" ...
df[char_vars_names][1] # "val2" "val2" "val1" ...
df[[char_vars_names]][1] # Error in .subset2(x, i) : recursive indexing failed at level 2
– Karl Baker
Nov 24 '18 at 0:01
I get an error when I use the double bracket below, but maybe I didn't follow your suggestion correctly:
char_vars_names # [1] "var1" "var2" ...
char_vars_names[1] # "var1"
# Works/works/doesn't work: df$var1 # "val2" "val2" "val1" ...
df[char_vars_names][1] # "val2" "val2" "val1" ...
df[[char_vars_names]][1] # Error in .subset2(x, i) : recursive indexing failed at level 2
– Karl Baker
Nov 24 '18 at 0:01
Thanks for the tip on the for loop. That is exactly what I'll need as soon as I can get the rest of the code to work!
– Karl Baker
Nov 24 '18 at 0:10
Thanks for the tip on the for loop. That is exactly what I'll need as soon as I can get the rest of the code to work!
– Karl Baker
Nov 24 '18 at 0:10
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452834%2fwhat-r-object-can-hold-variables-so-i-can-iterate-over-the-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452834%2fwhat-r-object-can-hold-variables-so-i-can-iterate-over-the-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Try
df[[char_vars_names]][1]
. The$
notation can't take advantage of variable substitution, but the double brackets can.– jdobres
Nov 23 '18 at 21:06
3
Also your use of
range
will not work (I'm guessing you're coming from Python).for (i in char_vars_names)
might be what you're after.– jdobres
Nov 23 '18 at 21:07
I get an error when I use the double bracket below, but maybe I didn't follow your suggestion correctly:
char_vars_names # [1] "var1" "var2" ...
char_vars_names[1] # "var1"
# Works/works/doesn't work:df$var1 # "val2" "val2" "val1" ...
df[char_vars_names][1] # "val2" "val2" "val1" ...
df[[char_vars_names]][1] # Error in .subset2(x, i) : recursive indexing failed at level 2
– Karl Baker
Nov 24 '18 at 0:01
Thanks for the tip on the for loop. That is exactly what I'll need as soon as I can get the rest of the code to work!
– Karl Baker
Nov 24 '18 at 0:10