0

I'm trying to make use of the following

hd <- read.csv("mydata.csv") #read data into hd

x <- c("test_column1","test_column2") #Our vector with string names

test <- test = (head(x,2)) #this returns "test_column2"

barplot(100*prop.table(table(hd$test)))

I'm getting error "(converted from warning) no non-missing arguments to min; returning Inf" I assume that is because it thinks that the variable is an integer. Can someone explain to me?

This would work if it were (table(hd$test_column1)). But I want to put in a variable after the $ so I can make a for loop to make multiple barplots

r2evans
  • 141,215
  • 6
  • 77
  • 149
R-Student
  • 1
  • 1
  • You can't use asterisks in variable names! And you can't use the $ notation in this way, but you can do `hd[, var_name]` where `var_name = "test_column2"` – Andrew Gustar Nov 06 '22 at 16:15
  • I inferred that your `**`s were intended to highlight certain portions of your code here in the Stack interface. This is generally not a good idea, as (1) we aren't always certain if that is your real code; and (2) it muddies up the syntax-highlighting of the interface, so the code just looks wrong. I've suggested an edit to take them out. If you want to highlight certain portions, please add comments on the line before/after or at the end of the line of code. – r2evans Nov 06 '22 at 17:50
  • It's not clear what you intend `test <- test = (head(x,2))` to be doing. The right two portions `test = (head(x,2))` should return `x` unchanged (since `length(x)` is two); and assignment operators in R pass the value through, so the value of `test = (head(x,2))` is that returned by `head(x,2)`, and that is assigned _again_ to `test` in `test <- ...`. So while it works, it (1) is extraneous and difficult to follow, (2) inefficient at best, (3) likely to go wrong in other uses, and (4) does _not_ return `"test_column2`" as your comment suggests. – r2evans Nov 06 '22 at 17:52
  • Finally, `hd$test` where `test` is a vector of names is not going to work, see https://stackoverflow.com/q/1169456/3358272 for a discussion of `[`, `[[`, and `$`. In your case, I think you need `table(hd[test])`. – r2evans Nov 06 '22 at 17:53
  • Please confirm that the above comment resolves your issue(s), R-Student. I suspect strongly this is effectively a dupe of that link, but I don't want to dupe-hammer the question without some more confirmation. – r2evans Nov 06 '22 at 17:53
  • Thank you so much for your contributions R2evans and Andrew! Using hd[,var_name] solves this issue.I'll be sure to check out the link! – R-Student Nov 06 '22 at 19:06

0 Answers0