help with acessing matrix data

Hi everybody, I am a beginner in R and am attempting to run a one way ANOVA (edit: after some thought I may be better off running a chi squared analysis instead.) in R and have started with creating an array holding my data that looks like the following. (I'm not looking for a solution, I am just looking for insight into how to access the matrix data)
TRT nonLT ser none total
GB 81 46 5253 5380
SG 31 19 804 854
BP 606 325 8110 9041

the treatments are types of surgeries I have labeled as GB, SG, and BP. nonLT, ser, and none are the responses (non life threatening, serious, and none).
I made a matrix in R and gave my rows and columns names, but am lost at how to assign treatments. I am not sure how to access the row/column data if not by using my assigned names for them. I keep getting told that "GB" or other things can't be found. I attempted to assign treatments using "treat1=z$New[z$Trt=="GB"] and am told that "Error in z$New : $ operator is invalid for atomic vectors"

So I'm not sure if there is a way that I can change my data in R that would allow me to use that command?

You are getting an error because you are trying to subset a column named New and you don't have it defined in your dataframe, look at this example, your code works if it's applied to columns that you have already defined.

z <- data.frame(stringsAsFactors = FALSE,
                nonLT = c(81L, 31L, 606L),
                ser = c(46L, 19L, 325L),
                none = c(5253L, 804L, 8110L),
                total = c(5380L, 854L, 9041L),
                TRT = c("GB", "SG", "BP"))
z$nonLT[z$TRT == "GB"]
#> [1] 81

z[z$TRT == "GB",]
#>   nonLT ser none total TRT
#> 1    81  46 5253  5380  GB

Created on 2019-04-21 by the reprex package (v0.2.1.9000)

So, what I'd recommend doing, is jumping straight onto the R for Data Science wagon and never look back :+1:

# Load libraries
library('tidyverse')

# Define data
my_data = tribble(~TRT, ~nonLT, ~ser, ~none, ~total,
                  'GB', 81,  46,  5253, 5380,
                  'SG', 31,  19,  804,  854,
                  'BP', 606, 325, 8110, 9041)

# See only treatment 'GB'
my_data %>% filter(TRT == 'GB')

# Get value for treatment 'GB' and 'nonLT'
my_data %>% filter(TRT == 'GB') %>% select(nonLT) %>% pull

Hope it helps :slightly_smiling_face:

1 Like

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.