R Data.Table Mode Imputation First Record By Group

bvowe
data=data.frame("StudentID"=c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4),
"Time"=c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5),
"Group"=c(1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0),
"Class"=c(1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1),
"Test"=c(NA,1,0,NA,1,1,1,1,0,0,1,0,1,0,1,1,1,0,0,0),
"Score"=c(0,1,1,0,1,NA,0,1,NA,0,NA,1,1,1,1,0,0,1,1,1),
"P"=c(NA,3,1,1,1,1,2,NA,3,1,3,3,2,2,2,NA,NA,1,2,2))

Group-Pはカテゴリです。

data1:テスト、スコア、およびPのモードをグループおよびクラスごとに個別に計算してから、時間= 1のみのモードを代入したいと思います。

data2:別のステップとしてdata2を作成したいと思います。data2はdata1を取得し、T> 1である時間Tで欠落している値については、変数TestおよびScoreの各グループについて上記の値をコピーします。

data.tableソリューションに到達することを期待して!

akrun

ここModeから機能を利用できます

Mode <- function(x) {
  ux <- unique(x)
   ux[which.max(tabulate(match(x, ux)))]
}

次に、対象の列をループして、「モード」を「グループ」replaceごとに計算します。「モード」NAと「時間」は1です。

library(data.table)
nm1 <- c("Test", "Score", "P")
setDT(data)[ , (nm1) := lapply(.SD, function(x) 
    replace(x, is.na(x) & Time == 1, Mode(x))), by = .(Group), .SDcols = nm1]

2番目のケースでは、

library(zoo)
nm2 <- c("Test", "Score")
data[Time  > 1,  (nm2) := lapply(.SD, na.locf0), .SDcols = nm2, by = Group]

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

R Data.Table Copy First Value For Group

分類Dev

R Regression imputation on missing data

分類Dev

IOS/Objective-C: Load one record from Core Data without loading table first

分類Dev

R data.table: How to sum variables by group based on a condition?

分類Dev

R: data.table group and sum two columns

分類Dev

Finding the index of the maximum value by group for a data.table in R

分類Dev

R - Removing the first and last character of a every factor in a data.table

分類Dev

subtract from first row in a data.table in R

分類Dev

How to get particular record from a group in R

分類Dev

R data table unique record count based on all combination of a given list of values from 2 columns

分類Dev

not able to display record count in data table

分類Dev

Use LINQ to Group data from data table

分類Dev

Pandas group data until first ocurrence of a pattern

分類Dev

How to change type of target column when doing := by group in a data.table in R?

分類Dev

R: Efficiently extract rows with different element in specified column by group in data.table

分類Dev

R data.table: Rebase each group within the panel by a value found in another column

分類Dev

T SQL Group column with accumulating data in one record

分類Dev

Count rows in data table with certain values by group

分類Dev

Kafka Connect - Caused by: org.apache.kafka.connect.errors.ConnectException: PK mode for table is RECORD_KEY, but record key schema is missing

分類Dev

how to get first item from a group and prepare Data class object

分類Dev

Updating a data.table in R

分類Dev

R data.table "by" for "i"

分類Dev

Select columns in data table in R

分類Dev

R data.table rbindlist

分類Dev

Clear the variables except first row in a group after separate row R

分類Dev

removing the first 3 rows of a group with conditional statement in r

分類Dev

SQL Group by but record Relation

分類Dev

MYSQL GROUP BY RECORD

分類Dev

Returning earliest record in a group

Related 関連記事

  1. 1

    R Data.Table Copy First Value For Group

  2. 2

    R Regression imputation on missing data

  3. 3

    IOS/Objective-C: Load one record from Core Data without loading table first

  4. 4

    R data.table: How to sum variables by group based on a condition?

  5. 5

    R: data.table group and sum two columns

  6. 6

    Finding the index of the maximum value by group for a data.table in R

  7. 7

    R - Removing the first and last character of a every factor in a data.table

  8. 8

    subtract from first row in a data.table in R

  9. 9

    How to get particular record from a group in R

  10. 10

    R data table unique record count based on all combination of a given list of values from 2 columns

  11. 11

    not able to display record count in data table

  12. 12

    Use LINQ to Group data from data table

  13. 13

    Pandas group data until first ocurrence of a pattern

  14. 14

    How to change type of target column when doing := by group in a data.table in R?

  15. 15

    R: Efficiently extract rows with different element in specified column by group in data.table

  16. 16

    R data.table: Rebase each group within the panel by a value found in another column

  17. 17

    T SQL Group column with accumulating data in one record

  18. 18

    Count rows in data table with certain values by group

  19. 19

    Kafka Connect - Caused by: org.apache.kafka.connect.errors.ConnectException: PK mode for table is RECORD_KEY, but record key schema is missing

  20. 20

    how to get first item from a group and prepare Data class object

  21. 21

    Updating a data.table in R

  22. 22

    R data.table "by" for "i"

  23. 23

    Select columns in data table in R

  24. 24

    R data.table rbindlist

  25. 25

    Clear the variables except first row in a group after separate row R

  26. 26

    removing the first 3 rows of a group with conditional statement in r

  27. 27

    SQL Group by but record Relation

  28. 28

    MYSQL GROUP BY RECORD

  29. 29

    Returning earliest record in a group

ホットタグ

アーカイブ