DF <- data.frame(a=1:3, v1=4:6, v2=7:9, v3=10:12)
sub <- c("v1", "v2", "v3")
DF[, paste0("l", sub)] <- lapply(DF[, sub], log)
# a v1 v2 v3 lv1 lv2 lv3
# 1 1 4 7 10 1.386294 1.945910 2.302585
# 2 2 5 8 11 1.609438 2.079442 2.397895
# 3 3 6 9 12 1.791759 2.197225 2.484907
How to change multiple variable names in R?
質問
I am new to R
I am trying to create a variable list with variables containing the log of some other variables. I managed to create the list, but I don't know how to rename each variable. Moreover, I don't know how to make these variables be a part of my dataset. Here is what I do:
Imagine somedata is a csv file of the form:
v1, v2, v3, ..., vn
1, 4, 6, ..., 1
...
Then here is my script
##################
## Import Data
###################
lights <- read.csv("somedata.csv")
##################
## Variable Lists
###################
lights.varlist1 <- subset(lights, select=c(v1,v2,...,vJ))
###########
## Logs
###########
lights.logsvarlist1=apply(lights.varlist1, 2, function(y) log(y))
This part seems to be working just fine as the results of print(lights.logsvarlist1)[1,]
make sense
To change the names of the variables I do:
for (i in 1:length(lights.logsvarlist1[1,]) {
name <-paste("l", names(lights.varlist1)[i], separator="")
names(lights.logsvarlist1)[i]=name
}
I have now two problems.
When I
print(lights.logsvarlist1[1,]
, the names don't seem to have changed. I still have my old variable names as headers.When I
print(names(lights))
, my newly created variables don't seem to be part of the dataset (they are not in the list).
What am I doing wrong? I am very new to R and I really want to continue, I'd appreciate any help.
解決
他のヒント
This works for me and avoids the for loop
data = as.data.frame(matrix(abs(rnorm(100)), 10))
ldata = log(data)
names(ldata) = paste('log', names(ldata), sep = '')
Some other tips
apply(lights.varlist1, 2, function(y) log(y))
Can be replaced by
apply(lights.varlist1, 2, log)
As log is a function or simply
log(lights.varlist1)
Instead of the following
for (i in 1:length(lights.logsvarlist1[1,])
use
ncol(lights.logsvarlist1)
Your new variables aren't in the lights data frame. They are in a data frame called
lights.logsvarlist1
To put them in the data frame use merge or cbind. Type ?merge etc