2

id の行の値に応じて、data.frame に新しい列 (スロット) を追加しようとしています。短い代表的なデータは次のとおりです。

        id                Cap         Rs       R_inv
  257   464A485SSEE3    1.41E-10    736665.125  1.36E-06
  258   464A485SSEE3    1.30E-10    364822.6875 2.74E-06
  289   464A485TSEB2    1.44E-10    111996.1016 8.93E-06
  290   464A485TSEB2    1.33E-10    108541      9.21E-06

新しい列を追加するループ関数を作成できると思います。しかし、cbind や raw$slot<-??? などの簡単な関数を学びたいと思っています。

4

2 に答える 2

1

試す

 raw$slot <- with(raw, paste0("slot",as.numeric(factor(id))))
 raw
 #              id      Cap       Rs    R_inv  slot
 #257 464A485SSEE3 1.41e-10 736665.1 1.36e-06 slot1
 #258 464A485SSEE3 1.30e-10 364822.7 2.74e-06 slot1
 #289 464A485TSEB2 1.44e-10 111996.1 8.93e-06 slot2
 #290 464A485TSEB2 1.33e-10 108541.0 9.21e-06 slot2

または、データセットの順序が の場合、次のidこともできます

 raw$slot <- paste0("slot",cumsum(c(TRUE,raw$id[-1]!=raw$id[-nrow(raw)])))

アップデート

カスタム ラベルが必要な場合は、idfactor(そうでない場合) に変換し、必要な を指定labelsできます。

raw$slot <- with(raw,  as.character(factor(id, labels=c('split6', 'split9'))) )
raw$slot
#[1] "split6" "split6" "split9" "split9"

または、 をnumericに変換してインデックスfactornumeric利用し、そのインデックスのベクトルを使用namesします。ここで、これを行う前にレベルの順序を知る必要があります。

 with(raw, c('split6', 'split9')[as.numeric(factor(id))])
 #[1] "split6" "split6" "split9" "split9"

データ

 raw <- structure(list(id = c("464A485SSEE3", "464A485SSEE3", "464A485TSEB2", 
 "464A485TSEB2"), Cap = c(1.41e-10, 1.3e-10, 1.44e-10, 1.33e-10
 ), Rs = c(736665.125, 364822.6875, 111996.1016, 108541), R_inv = c(1.36e-06, 
 2.74e-06, 8.93e-06, 9.21e-06)), .Names = c("id", "Cap", "Rs", 
 "R_inv"), class = "data.frame", row.names = c("257", "258", "289", "290"))
于 2014-11-06T05:55:41.927 に答える
1

長いテーブルがある場合は、おそらく「dplyr」パッケージの「結合」機能の使用を検討できます。

# First, here is your data:
df <- structure(list(id = structure(c(1L, 1L, 2L, 2L), .Label = c("464A485SSEE3", 
"464A485TSEB2"), class = "factor"), Cap = c(1.41e-10, 1.3e-10, 
1.44e-10, 1.33e-10), Rs = c(736665.125, 364822.6875, 111996.1016, 
108541), R_inv = c(1.36e-06, 2.74e-06, 8.93e-06, 9.21e-06)), .Names = c("id", 
"Cap", "Rs", "R_inv"), class = "data.frame", row.names = c("257", 
"258", "289", "290"))

#               id      Cap       Rs    R_inv
# 257 464A485SSEE3 1.41e-10 736665.1 1.36e-06
# 258 464A485SSEE3 1.30e-10 364822.7 2.74e-06
# 289 464A485TSEB2 1.44e-10 111996.1 8.93e-06
# 290 464A485TSEB2 1.33e-10 108541.0 9.21e-06


# Then create a matching table similar to below:
match_table <- structure(list(id = structure(1:2, .Label = c("464A485SSEE3", 
"464A485TSEB2"), class = "factor"), slot_no = structure(1:2, .Label = c("slot_1", 
"slot_2"), class = "factor")), .Names = c("id", "slot_no"), class = "data.frame", row.names = c(NA, 
-2L))

#             id slot_no
# 1 464A485SSEE3  slot_1
# 2 464A485TSEB2  slot_2

# Do joining
library(dplyr)
left_join(df, match_table)
#             id      Cap       Rs    R_inv slot_no
# 1 464A485SSEE3 1.41e-10 736665.1 1.36e-06  slot_1
# 2 464A485SSEE3 1.30e-10 364822.7 2.74e-06  slot_1
# 3 464A485TSEB2 1.44e-10 111996.1 8.93e-06  slot_2
# 4 464A485TSEB2 1.33e-10 108541.0 9.21e-06  slot_2
于 2014-11-06T07:50:59.263 に答える