[英]R- Dataframe making a new dataframe from a old one
我使用的所有代碼如下:
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"))
我想創建一個新的 dataframe1,其中包含“StudyID”和“Score”,僅適用於 Result 等於“great”的人。 結果列應該只包含“StudyID”和“Score”,而不是 Result 列。
然后,我想為具有“StudyID”和“Score”列的 Result 等於“bad”的人創建另一個新的 dataframe2。
非常感謝您的幫助!
我會簡單地嘗試
df1 <- df[df$Result=='great',1:2]
df2 <- df[df$Result=='bad',1:2]
以便:
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
df2
也是如此。 有幫助嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.