#This is the example data
dataa <- data.frame(x1 = 1:5, # Create example data
x2 = 6:10,
x3 = 11:15,
x4 = c(1,1,2,2,2))
#Grouped by X4, I want to replace the minimum value in X1:X2 with the minimum value/2, which #means the 1 in X1 and 3 in X1 should be replaced with 0.5 and 1.5; 6 in X2 and 8 in X2 should be #replaced with 3 and 4, respectively.
#Because I will be calculate this in a big data with numerous colomuns, thus maybe I need a FOR #LOOP? However, my codes did not work:
for (j in 1:2)
{
for(i in 1:2)
{
if(dataa[,i]==min(dataa[,i]) & dataa$x4==j) #Wrong
{
dataa[,min(dataa[,i])]<-min(dataa[,i])/(sqrt(2))
}
}
}
#Can you help me?
Grouped by a specific variable, I want find the minimum value in the other columns and replace it
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
关注 If it helps, please click accept,Thanks!
try this:#This is the example data dataa <- data.frame(x1 = 1:5, # Create example data x2 = 6:10, x3 = 11:15, x4 = c(1,1,2,2,2)) #Grouped by X4, I want to replace the minimum value in X1:X2 with the minimum value/2, which #means the 1 in X1 and 3 in X1 should be replaced with 0.5 and 1.5; 6 in X2 and 8 in X2 should be #replaced with 3 and 4, respectively. #Because I will be calculate this in a big data with numerous colomuns, thus maybe I need a FOR #LOOP? However, my codes did not work: for(j in 1:2) { for(i in 1:2){ tmin=min(dataa[,i][dataa$x4==j]) dataa[,i][dataa[,i]==tmin&dataa$x4==j]<-tmin/2 } } dataa
x1 x2 x3 x4
0.5 3 11 1
2.0 7 12 1
1.5 4 13 2
4.0 9 14 2
5.0 10 15 2本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 opencv图像处理,需要四个处理结果图
- ¥15 无线移动边缘计算系统中的系统模型
- ¥15 深度学习中的画图问题
- ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
- ¥15 Python报错怎么解决
- ¥15 simulink如何调用DLL文件
- ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
- ¥30 线性代数的问题,我真的忘了线代的知识了
- ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
- ¥188 需要修改一个工具,懂得汇编的人来。