Aditya Singh
2014-Dec-16 11:25 UTC
[R] Help sought with 1. converting factor to numeric; 2. creating new dataframe
Dear R-experts, I am relatively new to R. Please help me in converting a dataframe into a numeric and then creating a new dataframe. R-code attached: my_xtrain=read.table("./train/X_train.txt") my_xtest=read.table("./test/X_test.txt") my_merge_data=merge(my_xtrain,my_xtest,all=TRUE) ### my_merge_datad is a new dataframe in which I want my_merge_data to be numeric my_merge_datad=as.numeric(as.character(my_merge_data),na.rm=TRUE) ### my_m: I want to initialize a dataframe here my_m=dat() for (i in 1: 561) { ## here in my_m[1,i] I want to store the mean of all the columns, 1 to 561 my_m[1,i]= mean(my_merge_data[,i]) ## here in my_m[2,i] that is the second row of the new dataframe my_m I want to store the standard deviation of columns 1 to ##561 my_m[2,i]=std(my_merge_data[,i]) } Thanking you, Aditya
Michael Dewey
2014-Dec-16 12:06 UTC
[R] Help sought with 1. converting factor to numeric; 2. creating new dataframe
Comments in line On 16/12/2014 11:25, Aditya Singh via R-help wrote:> Dear R-experts, > > I am relatively new to R. Please help me in converting a dataframe into a numeric and then creating a new dataframe. > > R-code attached: > > my_xtrain=read.table("./train/X_train.txt") > my_xtest=read.table("./test/X_test.txt") > my_merge_data=merge(my_xtrain,my_xtest,all=TRUE)Without knowing what is in x_train.txt and x_test.txt it is hard to be sure but you might be able to avoid the conversion to factors (assuming that is what happened by using stringsAsFactors = FALSE or as.is or setting colClasses. ?read.table may help here.> > > ### my_merge_datad is a new dataframe in which I want my_merge_data to be numeric > my_merge_datad=as.numeric(as.character(my_merge_data),na.rm=TRUE) > >Probably unnecessary to intialise here.> ### my_m: I want to initialize a dataframe here > my_m=dat() > >The loop is probably avoidable. Why not use apply on the columns? mymean <- apply(my_merg_data, 2, mean) and simlarly for the standard deviation and then my_m <- cbind(mymean, mysd) Of course without the data I have not tested any of this and I may have misunderstood the problem but there may be enough hints here to get you started.> for (i in 1: 561) { > > ## here in my_m[1,i] I want to store the mean of all the columns, 1 to 561 > my_m[1,i]= mean(my_merge_data[,i]) > > ## here in my_m[2,i] that is the second row of the new dataframe my_m I want to store the standard deviation of columns 1 to ##561 > my_m[2,i]=std(my_merge_data[,i]) > } > > Thanking you, > Aditya > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > > ----- > No virus found in this message. > Checked by AVG - www.avg.com > Version: 2015.0.5577 / Virus Database: 4253/8746 - Release Date: 12/16/14 > >-- Michael http://www.dewey.myzen.co.uk