0

次のようなデータセットがあります。

structure(list(A = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", 
"25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", 
"36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", 
"47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", 
"58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", 
"69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", 
"80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", 
"91", "92", "93", "94", "95", "96", "97", "98", "99", "100", 
"101", "102", "103", "104", "105", "106", "107", "108", "109", 
"110"), class = "factor"), T = c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6
), X = c(528.04, 528.04, 528.04, 528.04, 528.04, 528.04), Y = c(10.32, 
10.32, 10.32, 10.32, 10.32, 10.32), V = c(0, 0, 0, 0, 0, 0), 
    GD = c(0, 0, 0, 0, 0, 0), ND = c(NA, 0, 0, 0, 0, 0), ND2 = c(NA, 
    0, 0, 0, 0, 0), TID = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("t1", 
    "t10", "t100", "t101", "t102", "t103", "t104", "t105", "t106", 
    "t107", "t108", "t109", "t11", "t110", "t12", "t13", "t14", 
    "t15", "t16", "t17", "t18", "t19", "t2", "t20", "t21", "t22", 
    "t23", "t24", "t25", "t26", "t27", "t28", "t29", "t3", "t30", 
    "t31", "t32", "t33", "t34", "t35", "t36", "t37", "t38", "t39", 
    "t4", "t40", "t41", "t42", "t43", "t44", "t45", "t46", "t47", 
    "t48", "t49", "t5", "t50", "t51", "t52", "t53", "t54", "t55", 
    "t56", "t57", "t58", "t59", "t6", "t60", "t61", "t62", "t63", 
    "t64", "t65", "t66", "t67", "t68", "t69", "t7", "t70", "t71", 
    "t72", "t73", "t74", "t75", "t76", "t77", "t78", "t79", "t8", 
    "t80", "t81", "t82", "t83", "t84", "t85", "t86", "t87", "t88", 
    "t89", "t9", "t90", "t91", "t92", "t93", "t94", "t95", "t96", 
    "t97", "t98", "t99"), class = "factor")), .Names = c("A", 
"T", "X", "Y", "V", "GD", "ND", "ND2", "TID"), row.names = c(NA, 
6L), class = "data.frame")

しっぽ:

structure(list(A = structure(c(110L, 110L, 110L, 110L, 110L, 
110L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8", "9", 
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", 
"21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", 
"32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", 
"43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", 
"54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", 
"65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", 
"76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", 
"87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", 
"98", "99", "100", "101", "102", "103", "104", "105", "106", 
"107", "108", "109", "110"), class = "factor"), T = c(180.5, 
180.6, 180.7, 180.8, 180.9, 181), X = c(380.12, 380.12, 380.12, 
378.4, 378.4, 378.4), Y = c(493.64, 493.64, 493.64, 493.64, 491.92, 
491.92), V = c(4.8649, 7.6921, 7.6921, 6.88, 3.44, 3.44), GD = c(1492.4688, 
1500.1609, 1507.853, 1514.733, 1518.173, 1521.613), ND = c(94.9745313228762, 
94.9745313228762, 94.9745313228762, 96.3814089957187, 97.3738774004609, 
97.3738774004609), ND2 = c(9020.16159999999, 9020.16159999999, 
9020.16159999999, 9289.376, 9481.67199999999, 9481.67199999999
), TID = structure(c(14L, 14L, 14L, 14L, 14L, 14L), .Label = c("t1", 
"t10", "t100", "t101", "t102", "t103", "t104", "t105", "t106", 
"t107", "t108", "t109", "t11", "t110", "t12", "t13", "t14", "t15", 
"t16", "t17", "t18", "t19", "t2", "t20", "t21", "t22", "t23", 
"t24", "t25", "t26", "t27", "t28", "t29", "t3", "t30", "t31", 
"t32", "t33", "t34", "t35", "t36", "t37", "t38", "t39", "t4", 
"t40", "t41", "t42", "t43", "t44", "t45", "t46", "t47", "t48", 
"t49", "t5", "t50", "t51", "t52", "t53", "t54", "t55", "t56", 
"t57", "t58", "t59", "t6", "t60", "t61", "t62", "t63", "t64", 
"t65", "t66", "t67", "t68", "t69", "t7", "t70", "t71", "t72", 
"t73", "t74", "t75", "t76", "t77", "t78", "t79", "t8", "t80", 
"t81", "t82", "t83", "t84", "t85", "t86", "t87", "t88", "t89", 
"t9", "t90", "t91", "t92", "t93", "t94", "t95", "t96", "t97", 
"t98", "t99"), class = "factor")), .Names = c("A", "T", "X", 
"Y", "V", "GD", "ND", "ND2", "TID"), row.names = 80419:80424, class = "data.frame")

Aに応じてデータ内のTの値を変更したい。データの範囲は 0.1 ~ 181 で、間隔は 0.1 です。各 A について、T は常に 0.1 から始まるとは限りません。これが私がやりたいことです。(したがって、A が変更されるたびに、T は常に 0.1 から開始されます)。最初に T=0 を作ってみました:

> NM$T=NULL

それから私はsapplyを使ってみましたが、惨めに失敗しました。どんな助けでも素晴らしいでしょう。

ありがとう

4

2 に答える 2

3

いつものように、これは で最も簡単に実行できdata.tableます:

library(data.table)
dt = data.table(your_df)

dt[, T := seq(from = 0.1, by = 0.1, length.out = .N), by = A]

次に例を示します。

dt = data.table(A = c(1,1,1,2,2), B = 2)
dt[, T := seq(from = 0.1, by = 0.1, length.out = .N), by = A]
dt
#   A B   T
#1: 1 2 0.1
#2: 1 2 0.2
#3: 1 2 0.3
#4: 2 2 0.1
#5: 2 2 0.2
于 2013-06-19T16:21:46.477 に答える