How to impute values row-wise in a dataframe?

r

#1

Hi everyone,

I am new to the field of data science and would need your help in executing the task. While working on a dataset, I observed that I had to perform operations only on some particular rows. Thus, I decided to make a dataframe and transfer all those rows in the new dataframe

I wanted to impute rows into a new dataframe based on a condition. So, I initialised it as follows:

df<-NULL

Next, I applied the condition such that the row with Student_ID=7697727 gets transferred into the first row of df.

df[1]<-a[which(a$Student_ID==7697727),]
Error in df[1] <- a[which(a$Student_ID == 7697727), ] :
object of type ‘closure’ is not subsettable

Please suggest an alternate method to do this.


#2

I think you can do the operations like following ways,
1)df1<-a[which(a$Student_ID==7697727),]
df2<-a[which(a$Student_ID==(another id),]
library(plyr)
df<-rbind.fill(df1,df2)
2)let’s say create a new column flag likethe following way,

a$flag<-ifelse(a$Student_ID==7697727|a$Student_ID==…|a$Student_ID=…,1,0)#it will identify the rows you want to select
Now,
df<-a[which(a$flag==1),]
df$flag=NULL #remove flag from subsetted df.
Thanks.


#3

@Prateek123

Hi there,

This type of error generally occurs when you try to subset a function. You can reproduce it using:

> mean[1]
Error in mean[1] : object of type 'closure' is not subsettable

What you might want to do is:

df<-null
IDs<-list(id1,id2,....)
sapply(IDs,function(x) { df<-rbind(df,a[which(a$Student_ID==x)])})

Hope this helps you.

Neeraj


#4

Thanks @Tapojyoti_Paul and @NSS.

I was able to apply row wise imputation successfully by your help.