R move named column to the end of a data frame - r
I'm trying to move a column to the end of a data frame and I'm struggling
output_index <- grep(output, names(df))
df <- cbind(df[,-output_index], df[,output_index])
This orders the data properly, however it converts the data to a matrix which doesn't work. How can I do this without losing the column names and keeping the data as a data frame.
Didn't need the , in front of the index:
output_index <- grep(output, names(df))
df <- cbind(df[-output_index], df[output_index])
df <- data.frame(id=1:10, output=rnorm(10,1,1), input=rnorm(10,1,1))
output_index <- grep("output", names(df))
res.df <- cbind(df[,-output_index], df[,output_index])
Related
column names on data frames
Does anybody know how to avoid the following problem ? dat <- as.data.frame(matrix(1:20, nrow=10)) names(dat) <- c("TEST","eval_12") dat$eval_1 dat$"eval_1" dat[,"eval_1"] As far as I understand, "eval_1" is not a name of the data.frame
Can't reorder data frame columns by matching column names given in another column
I'm trying to re-order the variables of my data frame using the contents of a variable in another data frame but it's not working and I don't know why. Any help would be appreciated! # Starting point df_main <- data.frame(coat=c(1:5),hanger=c(1:5),book=c(1:5), bottle=c(1:5),wall=c(1:5)) df_order <- data.frame(order_var=c("wall","book","hanger","coat","bottle"), number_var=c(1:5)) # Goal df_goal <- data.frame(wall=c(1:5),book=c(1:5),hanger=c(1:5), coat=c(1:5),bottle=c(1:5)) # Attempt df_attempt <- df_main[df_order$order_var]
In you df_order, put stringsAsFactors = FALSE in the data.frame call.
The issue is that you have the order as a factor, if you change it to a character it will work: df_goal <- df_main[as.character(df_order$order_var)]
Generate column names dynamically for a dataframe in R
So, I am coverting a json into dataframe using and I'm successful in doing that. Below is my code: df <- data.frame(t(sapply(json, c))) colnames(df) <- gsub("X", "y",colnames(df)) So, it gives me column names like y1,y2,y3 etc. Is it possible if I could have these column names generated from 0 instead. So, the column names should be like y0,y1,y2 etc.
From the comments: df <- data.frame(t(sapply(json,c)) colnames(df) <- paste0("y", 0:(ncol(df)-1)) Or if you want padded zeros a <- seq(0,ncol(df)-1,1) colnames(df) <- sprintf("y%02d",a)
R Converting Data Frame from Long to Wide
I'm trying to convert a data frame from long to wide format, but I'm running into the same issue - I get NA's. I think there might be something wrong with the aggregate function I'm using: library(reshape2) library(plyr) ID_NUMERIC <- c(5525,5525,5525,5525,5525,8523,8523,8523,8523,8523,4569,4569,4569,4569,4569) SAMPLE_NAME <-c("HX44","HX44","HX44","HX44","HX44","RT5","RT5","RT5","RT5","RT5","OP1","OP1","OP1","OP1","OP1") DATE <- c(as.Date("1/1/2014","1/1/2014","1/1/2014","1/1/2014","1/1/2014","1/15/2014","1/15/2014","1/15/2014","1/15/2014","1/15/2014","1/3/2014","1/3/2014","1/3/2014","1/3/2014","1/3/2014")) ANALYSIS<- c("P8","P8","P8","P8","P8","P8","P8","P8","P8","P8","P8","P8","P8","P8","P8") COMPONENT_NAME <- c("Density","Gravity","C6","C7","C8","Density","Gravity","C6","C7","C8","Density","Gravity","C6","C7","C8") RESULT <- c(0.8593,33.13,2.1,2.3,2.2,0.8593,33.13,2.1,2.3,2.2,0.8593,33.13,2.1,2.3,2.2) NAME <- c("HX","HX","HX","HX","HX","RT","RT","RT","RT","RT","OP","OP","OP","OP","OP") first <- data.frame(ID_NUMERIC,SAMPLE_NAME,DATE,COMPONENT_NAME,ANALYSIS,RESULT,NAME) second <- ddply(first, .(COMPONENT_NAME), function(x){x$id=1:nrow(x);x}) last <- dcast(second, NAME+SAMPLE_NAME+DATE+ID_NUMERIC+ANALYSIS+id~COMPONENT_NAME, value.var="RESULT")
You could put the data into a matrix and work with it from there? ID_NUMERIC <- c(5525,5525,5525,5525,5525,8523,8523,8523,8523,8523,4569,4569,4569,4569,4569) SAMPLE_NAME <-c("HX44","HX44","HX44","HX44","HX44","RT5","RT5","RT5","RT5","RT5","OP1","OP1","OP1","OP1","OP1") COMPONENT_NAME<-c("Density","Gravity","C6","C7","C8","Density","Gravity","C6","C7","C8","Density","Gravity","C6","C7","C8") NAME<-c("HX","HX","HX","HX","HX","RT","RT","RT","RT","RT","OP","OP","OP","OP","OP") ANALYSIS<-c("P8","P8","P8","P8","P8","P8","P8","P8","P8","P8","P8","P8","P8","P8","P8") DATE <- c("1/1/2014","1/1/2014","1/1/2014","1/1/2014","1/1/2014","1/15/2014","1/15/2014","1/15/2014","1/15/2014","1/15/2014","1/3/2014","1/3/2014","1/3/2014","1/3/2014","1/3/2014") RESULT <- c(0.8593,33.13,2.1,2.3,2.2,0.8593,33.13,2.1,2.3,2.2,0.8593,33.13,2.1,2.3,2.2) data<-matrix(c(ID_NUMERIC,SAMPLE_NAME,DATE,ANALYSIS,COMPONENT_NAME,RESULT,NAME),nrow=7,byrow=TRUE) rownames(data)<-c("ID_NUMERIC","SAMPLE_NAME","DATE","ANALYSIS","COMPONANT_NAME","RESULT","NAME") View(data) data.frame(data)
Trying to add multiple sequentially number columns to data frame in r
I need to add 7 empty columns (to represent days of the week) to an existing data frame and especially helpfull if they can be preceeded by the word "Day" I have previously used 7 lines like this DF$'Day 1' <- '' DF$'Day 2' <- '' Is it possible to shorten this, possibly using a loop? eg. for(i in 1:7) {DF#'Day [i]' <- ''} Which obviously doesn't work otherwise I wouldn't need to be asking.
If you attempt to assign to non-existent columns then they just get created for you automagically. DF <- data.frame(x = 1:4, y = 'hi') days <- paste0('Day',1:7) DF[,days] <- NA
If you need to create an empty data frame then you can do the following: DF <- as.data.frame(matrix(nrow=0, ncol=20)) names(DF) <- paste("Val", 1:20, sep="")