KatrinH Heimann
2013-May-10 08:59 UTC
[R] Replacing values in a dataframe depending on several conditions
Dear all, I would like to replace some values in a dataframe (including the columns Action, Interval and Artificiality) depending on a combination of different conditions the command I tried is for (j in 1:nrow(data)) { if ((data[j,data$Action]=="s") & (data[j,data$Interval]=="l") & (data[j,data $Artificiality]!="a")) {data[j,data$Artificiality]<-"na"} else { if ((data[j,data$Action]=="l") & (data[j,data$Interval]=="s") & (data[j,data $Artificiality]!="a")) {data[j,data$Artificiality]<-"na"} } } R does not give me an error message, but it is also not doing anything... can somebody be of help? Thanks Katrin [[alternative HTML version deleted]]
Berend Hasselman
2013-May-10 09:40 UTC
[R] Replacing values in a dataframe depending on several conditions
On 10-05-2013, at 10:59, KatrinH Heimann <katrinheimann at gmail.com> wrote:> Dear all, > > I would like to replace some values in a dataframe (including the columns > Action, Interval and Artificiality) depending on a combination of different > conditions > > the command I tried is > > for (j in 1:nrow(data)) > > { > > if ((data[j,data$Action]=="s") & (data[j,data$Interval]=="l") & (data[j,data > $Artificiality]!="a")) {data[j,data$Artificiality]<-"na"} > > else { > > if ((data[j,data$Action]=="l") & (data[j,data$Interval]=="s") & (data[j,data > $Artificiality]!="a")) {data[j,data$Artificiality]<-"na"} > > } > > } > > R does not give me an error message, but it is also not doing anything... > can somebody be of help?Don't use data$Columnname to access the relevant column (it's most likely empty). Use data[j,"Action"] etc. See ?"[.data.frame" (which you can also find with ?"Extract" and navigate). And don't use data as name of your variable. Do ?data to find out why. Why are you using "na"? NA seems more appropriate. Berend
Berend Hasselman
2013-May-10 09:54 UTC
[R] Replacing values in a dataframe depending on several conditions
On 10-05-2013, at 10:59, KatrinH Heimann <katrinheimann at gmail.com> wrote:> Dear all, > > I would like to replace some values in a dataframe (including the columns > Action, Interval and Artificiality) depending on a combination of different > conditions > > the command I tried is > > for (j in 1:nrow(data)) > > { > > if ((data[j,data$Action]=="s") & (data[j,data$Interval]=="l") & (data[j,data > $Artificiality]!="a")) {data[j,data$Artificiality]<-"na"} > > else { > > if ((data[j,data$Action]=="l") & (data[j,data$Interval]=="s") & (data[j,data > $Artificiality]!="a")) {data[j,data$Artificiality]<-"na"} > > } > > }Addendum: You can also use data$Action[j] etc. Berend