Dear R Help,
I am trying to put together two columns of unequal length in a data frame. Unfortunately, so far I have been unsuccessful in the functions I have tried (such as cbind). The code I am currently using is : (I have highlighted the code that is not working)
y<- mydata[,2:75]
year <- mydata$Year
res <- data.frame()
for (i in 1:74){
y.val <- y[,i]
lake.lm= lm(y.val ~ year)
lake.res=residuals(lake.lm)
new.res <- data.frame(lake.res=lake.res)
colnames(new.res) <- colnames(y)[i]
#cbind doesn't work because of the unequal lengths of my data columns
res <- cbind(res, new.res)
print(res)
}
mydata is a csv file with "Year" from 1950 on as my first column and then each proceeding column has a lake name and a day of year (single number) in each row.
Please let me know if there is any more information I can provide as I am new to emailing in this list. Thank you for your time!
Bailey Hewitt
Merging two columns of unequal length
4 messages · Bailey Hewitt, Mark Sharp, Jeff Newmiller +1 more
I did not look at the code, but note the following. By definition, 1. You cannot highlight code in plan text, which is the format accepted by r-help. 2. You cannot have columns of different lengths in a dataframe. R. Mark Sharp, Ph.D. msharp at TxBiomed.org
On Dec 12, 2016, at 5:41 PM, Bailey Hewitt <bailster at hotmail.com> wrote:
Dear R Help,
I am trying to put together two columns of unequal length in a data frame. Unfortunately, so far I have been unsuccessful in the functions I have tried (such as cbind). The code I am currently using is : (I have highlighted the code that is not working)
y<- mydata[,2:75]
year <- mydata$Year
res <- data.frame()
for (i in 1:74){
y.val <- y[,i]
lake.lm= lm(y.val ~ year)
lake.res=residuals(lake.lm)
new.res <- data.frame(lake.res=lake.res)
colnames(new.res) <- colnames(y)[i]
#cbind doesn't work because of the unequal lengths of my data columns
res <- cbind(res, new.res)
print(res)
}
mydata is a csv file with "Year" from 1950 on as my first column and then each proceeding column has a lake name and a day of year (single number) in each row.
Please let me know if there is any more information I can provide as I am new to emailing in this list. Thank you for your time!
Bailey Hewitt
[[alternative HTML version deleted]]
______________________________________________ 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.
CONFIDENTIALITY NOTICE: This e-mail and any files and/or...{{dropped:10}}
You can't do that. You can either make a different data frame, or you can stack the data in additional rows. If you make your example reproducible, we may be able to give more specific help. Also, post in plain text to avoid HTML code corruption. http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example http://adv-r.had.co.nz/Reproducibility.html
Sent from my phone. Please excuse my brevity.
On December 12, 2016 3:41:01 PM PST, Bailey Hewitt <bailster at hotmail.com> wrote:
>Dear R Help,
>
>
>I am trying to put together two columns of unequal length in a data
>frame. Unfortunately, so far I have been unsuccessful in the functions
>I have tried (such as cbind). The code I am currently using is : (I
>have highlighted the code that is not working)
>
>
>y<- mydata[,2:75]
>
>year <- mydata$Year
>
>res <- data.frame()
>
>for (i in 1:74){
>
> y.val <- y[,i]
>
> lake.lm= lm(y.val ~ year)
>
> lake.res=residuals(lake.lm)
>
> new.res <- data.frame(lake.res=lake.res)
>
> colnames(new.res) <- colnames(y)[i]
>
>#cbind doesn't work because of the unequal lengths of my data columns
>
> res <- cbind(res, new.res)
>
> print(res)
>
>}
>
>
>mydata is a csv file with "Year" from 1950 on as my first column and
>then each proceeding column has a lake name and a day of year (single
>number) in each row.
>
>
>Please let me know if there is any more information I can provide as I
>am new to emailing in this list. Thank you for your time!
>
>
>Bailey Hewitt
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>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.
2 days later
Hi Bailey,
I may be misunderstanding what you are doing as I can't work out how
you get unequal column lengths, but this may help:
myval<-matrix(sample(1:365,740,TRUE),ncol=74)
mydata<-as.data.frame(cbind(1950:1959,myval))
lakenames<-paste(rep(LETTERS[1:26],length.out=74),
rev(rep(letters[1:25],length.out=74)),
rep(letters[1:24],length.out=74),sep="")
names(mydata)<-c("Year",lakenames)
res<-vector("list",74)
for (i in 1:74){
thisval<-mydata[,i+1]
lake.lm<-lm(thisval ~ Year,mydata)
res[[i]]<-residuals(lake.lm)
}
# this will cause an error with as.data.frame
res[[20]]<-res[[20]][-1]
# pad the short column with NA
res<-lapply(res,function(x,length.out) x[1:length.out],10)
res.df<-as.data.frame(res)
names(res.df)<-lakenames
print(res.df)
Jim
On Tue, Dec 13, 2016 at 10:41 AM, Bailey Hewitt <bailster at hotmail.com> wrote:
Dear R Help,
I am trying to put together two columns of unequal length in a data frame. Unfortunately, so far I have been unsuccessful in the functions I have tried (such as cbind). The code I am currently using is : (I have highlighted the code that is not working)
y<- mydata[,2:75]
year <- mydata$Year
res <- data.frame()
for (i in 1:74){
y.val <- y[,i]
lake.lm= lm(y.val ~ year)
lake.res=residuals(lake.lm)
new.res <- data.frame(lake.res=lake.res)
colnames(new.res) <- colnames(y)[i]
#cbind doesn't work because of the unequal lengths of my data columns
res <- cbind(res, new.res)
print(res)
}
mydata is a csv file with "Year" from 1950 on as my first column and then each proceeding column has a lake name and a day of year (single number) in each row.
Please let me know if there is any more information I can provide as I am new to emailing in this list. Thank you for your time!
Bailey Hewitt
[[alternative HTML version deleted]]
______________________________________________ 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.