根据因素更改变量值

时间:2013-06-19 16:04:07

标签: r data-management

我的数据集如下所示:

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但是失败了。 任何帮助都会很棒。

由于

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

如果您希望T是从0.1开始的序列,并且为A列定义的每个组增加0.1,则可以使用以下代码:

DF$A <- sample(LETTERS[1:3],20,T)  # fake data

#> DF$A
# [1] "B" "C" "C" "C" "B" "B" "A" "C" "A" "C" "B" "C" "B" "B" "A" "C" "C" "A" "A" "C"

DF$T <- ave(as.integer(DF$A), DF$A, FUN=function(x)seq(from=0.1,by=0.1,length.out=length(x)))

结果:

> DF[order(DF$A),]
   A   T
1  A 0.1
2  A 0.2
3  A 0.3
4  A 0.4
5  A 0.5
6  B 0.1
7  B 0.2
8  B 0.3
9  B 0.4
10 B 0.5
11 B 0.6
12 C 0.1
13 C 0.2
14 C 0.3
15 C 0.4
16 C 0.5
17 C 0.6
18 C 0.7
19 C 0.8
20 C 0.9