Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

r - Get the value of a cell, not the index

In R, my dataset paym_area has 3 rows which each represent a person and the columns contain the payments required by each person to enrol 3, 2, 1 and 0 units of area into a scheme. For each of these people, I've identified the column that contains the max area they are willing to enrol for the given payment rate (paym_sh1a) using:

paym_3<-c(20,30,40)

paym_2<-c(12,32,34)

paym_1<-c(8,40,20)

paym_0<-c(0,0,0)

paym_area=cbind(paym_3,paym_2,paym_1,paym_0)

paym_sh1a=20

sums=apply(paym_area,1,function(x){x[x<=paym_sh1a]<-min(x);which.min(x)})

so this gives me an output of "[1] 1 4 3" But as well as the column index, I also want to know the value that is in each of those columns, how do I do this? so I want an output of "[1] 20 0 20"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can change your apply code to :

apply(paym_area,1,function(x)max(x[x<=paym_sh1a]))
#[1] 20  0 20

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...