简体   繁体   中英

R (studio) factor with levels

I'm a student currently learning to work with R (studio), for this I received a task. I am supposed to compare some mostly random generated data and draw a conclusion from this.

The problem I'm having however is the fact that this data has a factor with 5 levels and i want to compare the data one level at a time...

> str(data)
'data.frame':   275 obs. of  5 variables:  
 $ leverancier        : Factor w/ 5 levels "A","B","C","D",..: 1 1 1 1 1 1 1 1 1 1 ...   
 $ las_trekproef      : num  211 375 503 195 221 ...  
 $ las_score          : num  2.6 3.1 4.3 2.6 2.7 3.3 3.9 2.2 2.7 2.7 ...  
 $ afwijking_draaiwerk: num  0.081 0.061 0.015 0.125 0.256 0.004 0.124 0.016 0.042 0.062 ...  
 $ afwijking_freeswerk: num  0.336 0.026 0.032 0.161 0.36 0.447 0.062 0.176 0.317 0.212 ...

What I want to do is put the data for each level in a different variable so I can make graph's and box-plots one level at a time.

A <- (all data for level A here)

Instead of:

summary(data)  
 leverancier las_trekproef     las_score     afwijking_draaiwerk afwijking_freeswerk
 A:55        Min.   :128.6   Min.   :2.000   Min.   :0.0000      Min.   :0.0010     
 B:55        1st Qu.:270.6   1st Qu.:2.700   1st Qu.:0.0355      1st Qu.:0.1210     
 C:55        Median :361.0   Median :3.500   Median :0.0760      Median :0.2670     
 D:55        Mean   :356.1   Mean   :3.513   Mean   :0.1268      Mean   :0.3055     
 E:55        3rd Qu.:443.6   3rd Qu.:4.250   3rd Qu.:0.1340      3rd Qu.:0.4255     
             Max.   :571.4   Max.   :5.000   Max.   :1.1390      Max.   :1.3890

Thanks in advance,

NH

you mean something like

a <- subset(data, leverancier=='A')

for help simply check out ?subset

You could use tapply for your summary by factor. Boxplot will plot variable by factor if you use the formula interface and x is a factor. You could also index your factor to subset y to generate a single boxplot. However, for comparison of y based on factors you want to plot the levels on the same plot. Here are some examples.

# Create example data
dat <- data.frame(leverancier=rep(c("A","A","B","B","B","A","C","D","D","C"),100),
                  las_trekproef=runif(1000,100,500),
                  las_score=runif(1000,1,4))

# Use tapply to summarize y by factor                 
tapply(dat$las_score, dat$leverancier, FUN=summary)

# Using formula interface plot y by factor 
boxplot(las_score ~ leverancier, data=dat, notch=TRUE)

# You can also index y based on a factor level to create a single boxplot of y
boxplot(dat[dat$leverancier == "A" ,]$las_score, notch=TRUE)

Consider working with split

split(data, data$leverancier)

will give you a list of data.frame s, each of which corresponds to one level of leverancier . You can then operate on each element at a time, or loop over the list to operate on each part in turn.

I realize this does not directly answer your question (Seb's answer does that), but it should point you in a more idiomatic direction for working with data in R.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM