简体   繁体   English

R- Dataframe 从旧的 dataframe

[英]R- Dataframe making a new dataframe from a old one

All the code I used is below:我使用的所有代码如下:

df <- data.frame("StudyID" = paste("Study", seq(1:100), sep = "_"),
             "Score" = sample(c(-15:30),100, replace = TRUE))


df$Result<- ifelse(df$Score > 20, "great",
                 ifelse(df$Score < -5, "bad", "neutral"))

I want to create a NEW dataframe1 that contains the "StudyID" and "Score" only for people who have a Result that is equal to "great".我想创建一个新的 dataframe1,其中包含“StudyID”和“Score”,仅适用于 Result 等于“great”的人。 The resulting columns should only contain "StudyID"and "Score" and not the Result column.结果列应该只包含“StudyID”和“Score”,而不是 Result 列。

Then I want to creat another NEW dataframe2 for the people who have a Result that is equal to "bad" with the columns "StudyID" and "Score".然后,我想为具有“StudyID”和“Score”列的 Result 等于“bad”的人创建另一个新的 dataframe2。

Your help is really appreciated!非常感谢您的帮助!

I would simply try我会简单地尝试

df1 <- df[df$Result=='great',1:2]
df2 <- df[df$Result=='bad',1:2]

so that:以便:

head(df1)
    StudyID Score
9   Study_9    26
12 Study_12    30
13 Study_13    29
15 Study_15    22
19 Study_19    23
25 Study_25    21

and the same for df2 . df2也是如此。 does it help?有帮助吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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