简体   繁体   English

R将函数应用于数据框的每一行,将结果存储在同一数据框的新列中

[英]R apply function to each row of dataframe, store result in new column of same dataframe

I want to apply a function to each row of a datafame and store the result in a new column in the same dataframe. 我想将一个函数应用于datafame的每一行,并将结果存储在同一dataframe中的新列中。 The function looks at the value of Age.At.Event and returns the Age.Category it falls in. 该函数查看Age.At.Event的值,并返回其所属的Age.Category。

Here is the dataframe: 这是数据帧:

dput(so_example)
structure(list(Age.At.Event = c(4L, 9L, 7L, 13L, 13L, 13L, 11L, 
11L, 14L, 4L, 15L, 14L, 3L, 12L, 12L, 8L, 13L, 11L, 11L, 11L), 
    Dosage = c(4.9, 0, 3.9, 2.54, 5.51, 24.75, 4.99, 36.59, 2.69, 
    0.83, 2.36, 45.01, 0.96, 1.53, 0.97, 1.2, 4.96, 38.99, 5.95, 
    0), Dosage.typ = structure(c(10L, 7L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L), .Label = c("", "CGYCM2", "DGYCM2", "DLP", "GYCM2", 
    "MGY", "MGYCM", "MGYCM2", "MGYM2", "UGYM2"), class = "factor"), 
    kVp = c(70, 0, 66, 0, 0, 70, 70, 80, 63, 70, 66, 0, 70, 85, 
    60, 90, 70, 80, 70, 70), mAs = c(2, 0, 1.2, 0, 0, 2, 1.1, 
    4.9, 1, 1.6, 0.9, 0, 2, 1.7, 0.9, 1.4, 2, 3.2, 1.5, 1.5)), .Names = c("Age.At.Event", 
"Dosage", "Dosage.typ", "kVp", "mAs"), row.names = c(1L, 2L, 
3L, 4L, 5L, 6L, 8L, 9L, 10L, 15L, 16L, 17L, 18L, 19L, 20L, 22L, 
23L, 24L, 25L, 26L), class = "data.frame")

so_example
   Age.At.Event Dosage Dosage.typ kVp mAs
1             4   4.90      UGYM2  70 2.0
2             9   0.00      MGYCM   0 0.0
3             7   3.90      UGYM2  66 1.2
4            13   2.54      UGYM2   0 0.0
5            13   5.51      UGYM2   0 0.0
6            13  24.75      UGYM2  70 2.0
8            11   4.99      UGYM2  70 1.1
9            11  36.59      UGYM2  80 4.9
10           14   2.69      UGYM2  63 1.0
15            4   0.83      UGYM2  70 1.6
16           15   2.36      UGYM2  66 0.9
17           14  45.01      UGYM2   0 0.0
18            3   0.96      UGYM2  70 2.0
19           12   1.53      UGYM2  85 1.7
20           12   0.97      UGYM2  60 0.9
22            8   1.20      UGYM2  90 1.4
23           13   4.96      UGYM2  70 2.0
24           11  38.99      UGYM2  80 3.2
25           11   5.95      UGYM2  70 1.5
26           11   0.00      UGYM2  70 1.5

I've written a function which takes an input (a value from a specific column in each row) and returns a category based on that value (encoded as a character string). 我编写了一个函数,该函数接受输入(每行中特定列的值)并基于该值返回类别(编码为字符串)。 Here is my function: 这是我的功能:

ageCategories <- function(x){
  if(x < 1) "0-1"
  else if(x >= 1 & x < 4) "1-3"
  else if(x >= 4 & x < 8)  "4-7"
  else if(x >= 8 & x < 12) "8-11"
  else if(x >= 12 & x < 16) "12-16"
}

I want the output to look like this: 我希望输出看起来像这样:

   Age.At.Event Dosage Dosage.typ kVp mAs Age.Category
1             4   4.90      UGYM2  70 2.0   4-7
2             9   0.00      MGYCM   0 0.0  8-11
3             7   3.90      UGYM2  66 1.2   4-7
4            13   2.54      UGYM2   0 0.0 12-16
5            13   5.51      UGYM2   0 0.0 12-16
6            13  24.75      UGYM2  70 2.0 12-16
8            11   4.99      UGYM2  70 1.1  8-11
9            11  36.59      UGYM2  80 4.9  8-11
10           14   2.69      UGYM2  63 1.0 12-16
15            4   0.83      UGYM2  70 1.6   4-7
16           15   2.36      UGYM2  66 0.9 12-16
17           14  45.01      UGYM2   0 0.0 12-16
18            3   0.96      UGYM2  70 2.0   1-3
19           12   1.53      UGYM2  85 1.7 12-16
20           12   0.97      UGYM2  60 0.9 12-16
22            8   1.20      UGYM2  90 1.4  8-11
23           13   4.96      UGYM2  70 2.0 12-16
24           11  38.99      UGYM2  80 3.2  8-11
25           11   5.95      UGYM2  70 1.5  8-11
26           11   0.00      UGYM2  70 1.5  8-11

The function works fine with individual numeric inputs, but I can't seem to get it working on my rows in the dataframe. 该函数可以与单个数字输入正常工作,但是我似乎无法在数据框中的行上使用它。

I've tried calling it in a couple of different ways , shown below, but am getting a bit stuck. 我尝试用几种不同的方式调用它,如下所示,但是有点卡住了。 I suspect my answer lies within the plyr package, but haven't had any luck getting this to work either. 我怀疑我的答案在plyr软件包内,但也没有运气使它起作用。 Can someone shed some light on what I'm doing wrong? 有人可以告诉我我在做什么错吗?

so_example$Age.Category <- apply(so_example, 1, ageCategories(.(Age.At.Event)))

Error in ageCategories(.(Age.At.Event)) : (list) object cannot be coerced to type 'double' ageCategories中的错误(。(Age.At.Event)):(list)对象不能被强制键入'double'

so_example[,Age.Category:=sapply(Age.At.Event,ageCategories)][]

Error in [.data.frame (so_example, , := (Age.Category, sapply(Age.At.Event, : could not find function ":=" [.data.frame (so_example,, := (Age.Category,sapply(Age.At.Event,:找不到函数“:=”

so_example$Age.Category <- cut(so_example$Age.At.Event, 
         breaks=c(-Inf, 1,3,7,11,16), labels=c('0-1', '1-3', '4-7', '8-11','12-16')) 

 so_example
#   Age.At.Event Dosage Dosage.typ kVp mAs Age.Category
#1             4   4.90      UGYM2  70 2.0          4-7
#2             9   0.00      MGYCM   0 0.0         8-11
#3             7   3.90      UGYM2  66 1.2          4-7
#4            13   2.54      UGYM2   0 0.0        12-16
#5            13   5.51      UGYM2   0 0.0        12-16
#6            13  24.75      UGYM2  70 2.0        12-16
#8            11   4.99      UGYM2  70 1.1         8-11
#9            11  36.59      UGYM2  80 4.9         8-11
#10           14   2.69      UGYM2  63 1.0        12-16
#15            4   0.83      UGYM2  70 1.6          4-7
#16           15   2.36      UGYM2  66 0.9        12-16
#17           14  45.01      UGYM2   0 0.0        12-16
#18            3   0.96      UGYM2  70 2.0          1-3
#19           12   1.53      UGYM2  85 1.7        12-16
#20           12   0.97      UGYM2  60 0.9        12-16
#22            8   1.20      UGYM2  90 1.4         8-11
#23           13   4.96      UGYM2  70 2.0        12-16
#24           11  38.99      UGYM2  80 3.2         8-11
#25           11   5.95      UGYM2  70 1.5         8-11
#26           11   0.00      UGYM2  70 1.5         8-11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM