基于其他变量重命名多行的值 [英] rename value for multiple rows based on other variable

查看:42
本文介绍了基于其他变量重命名多行的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,每个参与者都有多行.他们的一些 id 有错误(例如,有些是双重的).我想给他们分配一个这样的新

I've got a data frame with multiple rows for each participant. There is an error in some of their ids (for example, some are double). I wanted to assign them a new one like this

dat[dat$participant == "36" & dat$date == "2020-06-07_12h33.46.880"] <- "101"

我收到错误消息列的下标重复".我的命令有什么问题?

I get the error message "duplicate subscripts for columns". Whats wrong with my command?

我也试过

dat$participant[dat$date== "2020-06-07_12h33.46.880"] <- "101"

没有错误但也没有出现这些值的参与者

with no error but also no participant with these value appearing

推荐答案

duplicate subscripts for columns 错误在这里告诉您,您正在尝试为数据帧的无意义部分赋值.所以你需要使用 dat$participant 而不仅仅是 dat.

The error duplicate subscripts for columns tells you here that you are trying to assign a value to a nonsensical part of dataframe. So you need to use dat$participant rather than only dat.

一般来说,可以使用这个命令:

Generally speaking, you can use this command:

dat$participant[some condition] <- "101"

例如修改你的第一个命令:

E.g. modifying your first command:

dat$participant[dat$participant == "36" & dat$date == "2020-06-07_12h33.46.880"] <- "101"

但可能还有另一个问题,例如确保您提供正确的日期(复制并粘贴该值,因为有多种类型的连字符破折号) 可能出现在脏数据中,也许 ID 不是字符而是数字等.

But there can be another problem, e.g. make sure you provide correct date (copy and paste the value, as there are many types of hyphens and dashes) which can occur in dirty data, maybe the IDs are not characters but numbers, etc.

因此,如果您需要更具体的帮助,请在此处提供几行数据框.

So if you want more concrete help, provide here a few lines of your data frame.

这篇关于基于其他变量重命名多行的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆