Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
37
Help us understand the problem. What is going on with this article?
@dokokano_panda

R:データフレームの列を入れ替える・削除する。

More than 5 years have passed since last update.

データフレームの項目の順序を入れ替えたり削除したりする方法です。
いくつか方法があるけど、これが個人的に一番楽かな。
毎度おなじみirisさんで確認。

最初の状態。

> iris[1,]
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa

1列目と2列目の入れ替え。

> iris2 <- iris[,c(2,1,3,4,5)]
> iris2[1,]
  Sepal.Width Sepal.Length Petal.Length Petal.Width Species
1         3.5          5.1          1.4         0.2  setosa

1列目、2列目だけ残す。c(1:2)でも可。

> iris2 <- iris[,c(1,2)]
> iris2[1,]
  Sepal.Length Sepal.Width
1          5.1         3.5

1列目、2列目だけ残す。他の列を削除する形式。

> iris2 <- iris[,c(-3,-4,-5)]
> iris2[1,]
  Sepal.Length Sepal.Width
1          5.1         3.5
37
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
37
Help us understand the problem. What is going on with this article?