Reputation: 721
I have the following data frame.
V1 <- c(2, 2, 2)
V2 <- c(12, 13, 24)
V3 <- c(3, 3, 3)
V4 <- c(1, 23, 33)
data <- data.frame(V1, V2, V3, V4)
data
V1 V2 V3 V4
1 2 12 3 1
2 2 13 3 23
3 2 24 3 33
I want to rename the V1 based on the value in V1
. That means, I want rename the variable V2
as Var_2
where the number 2
comes from the V1
.
Var_2 Var_3
1 12 1
2 13 23
3 24 33
Any easy way to do that? I want to replicate the process for 500 variables. Is there any way avoiding loops?
Upvotes: 3
Views: 273
Reputation: 39858
Here is a tidyverse
possibility:
data %>%
rowid_to_column() %>%
gather(var, val, -rowid) %>%
arrange(rowid) %>%
mutate(temp = ifelse(parse_number(var) %% 2 == 0, 1, NA),
var2 = ifelse(temp == 1 & is.na(lag(temp, default = 0)),
paste("Var", lag(val), sep = "_"), NA)) %>%
na.omit() %>%
select(-var, -temp) %>%
spread(var2, val) %>%
select(-rowid)
Var_2 Var_3
1 12 1
2 13 23
3 24 33
First, it generates an unique row ID. Second, it transforms the data from wide to long format. Third, it checks whether the column names consist of even number. If so, it assigns 1, otherwise NA. Then, if it is an even number and the lag is NA, it combines "Var" and the value from the lagged column. Finally, it deletes the NA values and returns it back to wide format.
Upvotes: 1
Reputation: 13309
data.table
approach: For 500 variables you'll have to devise means of removing them not as shown here. Also it is assumed that all columns will have the same value ie 3333,2222 and so on.
data <- data.frame(V1, V2, V3, V4)
library(data.table)
setDT(data)
data1<-data[,list(V1,V3)]
#data1[,lapply(.SD,unique)]
data[,`:=`(V1=NULL,V3=NULL)]
names(data)<-do.call("paste",list("Var_",unique(data1)))
data
Var_ 2 Var_ 3
1: 12 1
2: 13 23
3: 24 33
Upvotes: 1
Reputation: 570
Maybe I'm not getting your question, but as long as you're just needing the names from the first row of your data, something like this should work:
names(data) <- paste("Var",data[1,][1:ncol(data)],sep="_")
> names(data)
[1] "Var_2" "Var_12" "Var_3" "Var_1"
Upvotes: 1