admin管理员组文章数量:1402775
I wish to add some columns to my original data frame whilst looping through the rows in a loop. My issue is that when looping through I cant save out my dataframe after the cbind()
my position as the rest of my dataset has an unequal number of columns, i tried to combat this by creating the column names first and NA-ing them before looping through my data but still no joy. Some examples of what ive tried at the bottom. Note...I know the following example doesn't need a loop its just a subset of a bigger thing I'm working with
myDat
id name city
1 john london
2 james manchester
3 steve hull
4 david birmingham
myDat2
att1 att2
23 21
25 30
44 28
30 28
for(i in 1:nrow(myDat)){
#grab data that creates myDat 2 here then combine them
#in this instance myDat2 is just line 1
myDat2 = data.frame(att1 = 23, att2 = 21) #when i=1
myDat[i,] = cbind(myDat[i, ], mydat2)
}
error/warning message = number of items to replace is not a multiple of replacement length
I wish to add some columns to my original data frame whilst looping through the rows in a loop. My issue is that when looping through I cant save out my dataframe after the cbind()
my position as the rest of my dataset has an unequal number of columns, i tried to combat this by creating the column names first and NA-ing them before looping through my data but still no joy. Some examples of what ive tried at the bottom. Note...I know the following example doesn't need a loop its just a subset of a bigger thing I'm working with
myDat
id name city
1 john london
2 james manchester
3 steve hull
4 david birmingham
myDat2
att1 att2
23 21
25 30
44 28
30 28
for(i in 1:nrow(myDat)){
#grab data that creates myDat 2 here then combine them
#in this instance myDat2 is just line 1
myDat2 = data.frame(att1 = 23, att2 = 21) #when i=1
myDat[i,] = cbind(myDat[i, ], mydat2)
}
error/warning message = number of items to replace is not a multiple of replacement length
Share
Improve this question
asked Mar 21 at 14:12
JoeJoe
1,3975 silver badges21 bronze badges
2 Answers
Reset to default 2You can first add a list column and store pretty much any object at any index there. If that object happens to be a frame (or frame-like list, for example), you can later unnest it to a regular flat data.frame:
myDat$list_col <- vector(mode = "list", length = nrow(myDat))
for(i in 1:nrow(myDat)){
myDat2 = data.frame(att1 = 23, att2 = 21)
if (i %% 2 == 0) myDat2 = cbind(myDat2, att3 = 33)
myDat$list_col[[i]] = myDat2
}
myDat
#> id name city list_col
#> 1 1 john london 23, 21
#> 2 2 james manchester 23, 21, 33
#> 3 3 steve hull 23, 21
#> 4 4 david birmingham 23, 21, 33
tidyr::unnest_wider(myDat, list_col)
#> # A tibble: 4 × 6
#> id name city att1 att2 att3
#> <int> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 1 john london 23 21 NA
#> 2 2 james manchester 23 21 33
#> 3 3 steve hull 23 21 NA
#> 4 4 david birmingham 23 21 33
Example data:
myDat <- read.table(header = TRUE, text =
"id name city
1 john london
2 james manchester
3 steve hull
4 david birmingham")
myDat2 <- read.table(header = TRUE, text =
"att1 att2
23 21
25 30
44 28
30 28")
Another option might be to use data.table, which allows assigning a data.frame/data.table to columns in another table. For example:
library(data.table)
myDat = data.table(id = 1:4,
name=c("john", "james", "steve", "david"),
city=c("london", "manchester", "hull", "birmingham"))
for(i in 1:nrow(myDat)){
myDat2 <- data.frame(att1 = c(23, 25, 44, 30)[i], att2 = c(21, 30, 28, 28)[i])
myDat[i, c("att1", "att2"):=myDat2]
}
print(myDat)
Result
#> id name city att1 att2
#> <int> <char> <char> <num> <num>
#> 1: 1 john london 23 21
#> 2: 2 james manchester 25 30
#> 3: 3 steve hull 44 28
#> 4: 4 david birmingham 30 28
本文标签: rcbind() or merge() my data in 2 separate data frames whilst in a for loopStack Overflow
版权声明:本文标题:r - cbind() or merge() my data in 2 separate data frames whilst in a for loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744347450a2601846.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论