找到你要的答案

Q:How do i separate a group of data in R based on their name

Q:如何在R中根据自己的名字分离一组数据

I have csv with a set of cars in R. How do I set up the data to where one group contains three specific cars, and the other group is the rest of the cars? I have tried

carA=someintervalvariable[car=="carA"]
carB=someintervalvariable[car=="carB"]
carC=someintervalvariable[car=="carC"]
ABC=which(c("A","B","C"))
others=someintervalvariable[-ABC]

AND

group1 <- data$car["carA","carB","carC"]

I am stuck. I don't know how to collect those three cars while keeping the other cars in a different set. I would like to run tests on those cars vs. the other cars with ratio and interval data. How do i save them separately?

Here is an example of my data:

car         mpg         satisfaction   
 carA:1   Min.   :12.00   Min.   :0.2000  
 carB:1   1st Qu.:21.00   1st Qu.:0.3850  
 carC:1   Median :23.00   Median :0.5600  
 carD:1   Mean   :22.43   Mean   :0.5386  
 carE:1   3rd Qu.:24.50   3rd Qu.:0.7150  
 carF:1   Max.   :31.00   Max.   :0.8100  
 carG:1                                  

我在研究如何建立起一组包含三个特定的数据集汽车汽车的CSV格式,而另一组是其余的车吗?我已经试过了

carA=someintervalvariable[car=="carA"]
carB=someintervalvariable[car=="carB"]
carC=someintervalvariable[car=="carC"]
ABC=which(c("A","B","C"))
others=someintervalvariable[-ABC]

group1 <- data$car["carA","carB","carC"]

我卡住了。我不知道如何收集这三辆车,而另一辆车则不同。我想运行那些汽车与其他汽车的比率和间隔数据测试。如何分别保存?

这里是我的数据的一个例子:

car         mpg         satisfaction   
 carA:1   Min.   :12.00   Min.   :0.2000  
 carB:1   1st Qu.:21.00   1st Qu.:0.3850  
 carC:1   Median :23.00   Median :0.5600  
 carD:1   Mean   :22.43   Mean   :0.5386  
 carE:1   3rd Qu.:24.50   3rd Qu.:0.7150  
 carF:1   Max.   :31.00   Max.   :0.8100  
 carG:1                                  
answer1: 回答1:

I think that you have to provide a reproducible example because my thought are that your problem "might" (I bet it does) include regular expressions and so there is a huge amount of possibilities.

To start, Give a look at this code just ho have an idea and let's know if it useful for you. It allows you to select all car_X where X is all letters (capital and non capital) except the ones to d from z.

cars <- c("car_A", "car_B", "car_C", "car_D", "car_E")
car1 <- grep("car_[^d-zD-Z]", cars, value = TRUE )
car1
[1] "car_A" "car_B" "car_C"

Subset a Data Frame

With a data frame, then, you can subset based on the output of your grep; consider the following example, which continues the previous one.

values <- rnorm(5)
data <- data.frame(cars, values)
data1 <- data[grep( "car_[a-cA-C]", data[ ,1] ) , ]
> data1
   cars     values
1 car_A -1.8553913
2 car_B -0.3562586
3 car_C -0.3208530

我认为,你必须提供一个可重复的例子,因为我的想法是,你的问题“可能”(我打赌它)包括正则表达式,所以有大量的可能性。

首先,看看这个代码,只要他有一个想法,让我们知道,如果它对你有用。你可以选择所有的car_x X是所有字母(资本和非资本)除了Z D的

cars <- c("car_A", "car_B", "car_C", "car_D", "car_E")
car1 <- grep("car_[^d-zD-Z]", cars, value = TRUE )
car1
[1] "car_A" "car_B" "car_C"

Subset a Data Frame

一个数据帧,然后,你可以根据你的grep输出子集;考虑下面的例子,它延续了前一个。

values <- rnorm(5)
data <- data.frame(cars, values)
data1 <- data[grep( "car_[a-cA-C]", data[ ,1] ) , ]
> data1
   cars     values
1 car_A -1.8553913
2 car_B -0.3562586
3 car_C -0.3208530
r  grouping  category  subset