如何根据名称分隔R中的一组数据

时间:2022-09-19 07:36:00

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

我在CS中有一组汽车的csv。我如何将数据设置到一组包含三辆特定汽车的位置,另一组是剩下的汽车?我试过了

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                                  

1 个解决方案

#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.

首先,看看这段代码,我们就有了一个想法,让我们知道它对你有用。它允许您选择所有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

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

然后,使用数据框,您可以根据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

#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.

首先,看看这段代码,我们就有了一个想法,让我们知道它对你有用。它允许您选择所有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

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

然后,使用数据框,您可以根据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