0

I apologize, I posted this same question yesterday but phrased it very poorly. I have no idea how to go about approaching this problem. I have approximately 2000 rows of data, with an owner, percentage of credit to the owner, an employee, and a percentage of credit to the employee. I have provided an example row.

owner<- c("bob") percentage_owner<- .75 employee<- c("sydney") employee_percent<- .25 

For each row of data, I am attempting to create a new row of data that places the owner's name into the employee column, and his percentage as the employees percentage, keeping all other columns the same in the new row, like so:

owner2<- c("bob", "bob") percentage_owner2<- c(.75, .75) employee2<- c("sydney", "bob") employee_percent2<- c(.25,.75) sample_data<-data.frame(owner, percentage_owner, employee, employee_percent) goal_data <- data.frame(owner = owner2, percentage_owner = percentage_owner2, employee = employee2, employee_percent = employee_percent2) 

I want this to occur for all rows of the data, essentially doubling the dataset. How would I go about doing this? I don't have much experience using R for this sort of data manipulation. Any help is greatly appreciated!

2 Answers 2

1

The following function accepts a data.frame with the columns named as in the question and returns each row doubled in the special way the question asks for.

funDouble <- function(X){ f <- function(X){ owner <- rep(X[['owner']], 2) percentage_owner <- rep(X[['percentage_owner']], 2) employee <- c(X[['employee']], X[['owner']]) employee_percent <- c(X[['employee_percent']], X[['percentage_owner']]) data.frame(owner, percentage_owner, employee, employee_percent) } res <- lapply(row.names(X), function(i) f(X[i,, drop = FALSE])) res <- do.call(rbind, res) row.names(res) <- NULL res } owner<- "bob" percentage_owner <- 0.75 employee<- "sydney" employee_percent <- 0.25 df1 <- data.frame(owner, percentage_owner, employee, employee_percent) funDouble(df1) # owner percentage_owner employee employee_percent #1 bob 0.75 sydney 0.25 #2 bob 0.75 bob 0.75 
Sign up to request clarification or add additional context in comments.

Comments

0

Try this function made with base R:

#Function repeatfunc <- function(x,n) { #empty object Empty <- list() #Fill for(i in c(1:n)) { Empty[[i]] <- x } #Dataframe z <- do.call(rbind,Empty) return(z) } 

Using it:

repeatfunc(sample_data,5) owner percentage_owner employee employee_percent 1 bob 0.75 sydney 0.25 2 bob 0.75 sydney 0.25 3 bob 0.75 sydney 0.25 4 bob 0.75 sydney 0.25 5 bob 0.75 sydney 0.25 

You can save the results in a new dataframe if you wish.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.