如何将字符串添加到 R 数据 table 中一行的每个单元格?
How to add a string to each cell of a row in a R data table?
我遇到了一个看起来很基本的问题,但我找不到解决方案。
我有一个简单的 table :
Statut occupation
1983
1988
1996
2002
2007
2012
2017
Propriétaire du logement
207
267
305
363
468
597
482
Locataire
35
40
33
52
50
61
60
Locataire de l'habitat social (OPH, OTHS)
0
0
0
0
0
2
0
Logé gratuitement (parents, amis, employeurs)
39
47
69
99
57
87
98
Total général
281
354
407
514
575
745
640
我想得到这个结果:
Statut occupation
1983
1988
1996
2002
2007
2012
2017
Propriétaire du logement
207
267
305
363
468
597
482
Locataire
35
40
33
52
50
61
60
Locataire de l'habitat social (OPH, OTHS)
0
0
0
0
0
2
0
Logé gratuitement (parents, amis, employeurs)
39
47
69
99
57
87
98
Total général
281
354
407
514
575
745
640
目的只是在一行的所有单元格上添加格式(斜体、下划线、添加不可分割的空格...)。看起来在 R 中没那么容易。
我试过的
我尝试获取每列的名称并修改 for 循环中对应的单元格
n.row=3
cols=colnames(Y)
for (i in 1:ncol(Y)){
Y[n.row,get(cols[i])]<-as.data.table(sprintf("*%s*",as.character(Y[n.row,get(cols[i])])))
}
这里的问题是 Y[n.row,get(cols[i])] 总是 return "Statut occupation"(列名),无论 [=60 的值如何=].为什么?
我也尝试让它直接使用列的 id :
n.row=3
for (i in 1:ncol(Y)){
Y[n.row,..i]<-sprintf("*%s*",Y[n.row,..i])
}
此处:
- Y[n.row,..i] 给我正确的信息...
- sprintf("%s",as.character(Y[n.row,..i])) 无论 class 列...
- 但是 Y[n.row,..i]<-sprintf("%s",as.character(Y[n.row ,..i])) returns
Error in [<-.data.table
(*tmp*
, n.row, ..i, value = " Locataire de l'habitat social (OPH, OTHS)") :
object '..i' not found
我不明白这里的行为。所有信息都可以一一找到,但我无法将一个信息分配给另一个信息,因为它突然找不到任何信息。
任何解释将不胜感激,或者我可能没有使用正确的策略来做我需要的事情:) !
感谢您的帮助!
注意 *tmp*
是一个字符,因此所有列都应保持相同的类型。
library(data.table)
Y <- fread("Statut occupation 1983 1988 1996 2002 2007 2012 2017
Propriétaire du logement 207 267 305 363 468 597 482
Locataire 35 40 33 52 50 61 60
Locataire de l'habitat social (OPH, OTHS) 0 0 0 0 0 2 0
Logé gratuitement (parents, amis, employeurs) 39 47 69 99 57 87 98
Total général 281 354 407 514 575 745 640",header = T,colClasses = 'character')
n.row <- 3
Y[n.row, names(Y) := as.list(sprintf("*%s*",Y[n.row,]))]
输出:
Statut occupation 1983 1988 1996 2002 2007 2012 2017
<char> <char> <char> <char> <char> <char> <char> <char>
1: Propriétaire du logement 207 267 305 363 468 597 482
2: Locataire 35 40 33 52 50 61 60
3: *Locataire de l'habitat social (OPH, OTHS)* *0* *0* *0* *0* *0* *2* *0*
4: Logé gratuitement (parents, amis, employeurs) 39 47 69 99 57 87 98
5: Total général 281 354 407 514 575 745 640
我遇到了一个看起来很基本的问题,但我找不到解决方案。
我有一个简单的 table :
Statut occupation | 1983 | 1988 | 1996 | 2002 | 2007 | 2012 | 2017 |
---|---|---|---|---|---|---|---|
Propriétaire du logement | 207 | 267 | 305 | 363 | 468 | 597 | 482 |
Locataire | 35 | 40 | 33 | 52 | 50 | 61 | 60 |
Locataire de l'habitat social (OPH, OTHS) | 0 | 0 | 0 | 0 | 0 | 2 | 0 |
Logé gratuitement (parents, amis, employeurs) | 39 | 47 | 69 | 99 | 57 | 87 | 98 |
Total général | 281 | 354 | 407 | 514 | 575 | 745 | 640 |
我想得到这个结果:
Statut occupation | 1983 | 1988 | 1996 | 2002 | 2007 | 2012 | 2017 |
---|---|---|---|---|---|---|---|
Propriétaire du logement | 207 | 267 | 305 | 363 | 468 | 597 | 482 |
Locataire | 35 | 40 | 33 | 52 | 50 | 61 | 60 |
Locataire de l'habitat social (OPH, OTHS) | 0 | 0 | 0 | 0 | 0 | 2 | 0 |
Logé gratuitement (parents, amis, employeurs) | 39 | 47 | 69 | 99 | 57 | 87 | 98 |
Total général | 281 | 354 | 407 | 514 | 575 | 745 | 640 |
目的只是在一行的所有单元格上添加格式(斜体、下划线、添加不可分割的空格...)。看起来在 R 中没那么容易。
我试过的
我尝试获取每列的名称并修改 for 循环中对应的单元格
n.row=3
cols=colnames(Y)
for (i in 1:ncol(Y)){
Y[n.row,get(cols[i])]<-as.data.table(sprintf("*%s*",as.character(Y[n.row,get(cols[i])])))
}
这里的问题是 Y[n.row,get(cols[i])] 总是 return "Statut occupation"(列名),无论 [=60 的值如何=].为什么?
我也尝试让它直接使用列的 id :
n.row=3
for (i in 1:ncol(Y)){
Y[n.row,..i]<-sprintf("*%s*",Y[n.row,..i])
}
此处:
- Y[n.row,..i] 给我正确的信息...
- sprintf("%s",as.character(Y[n.row,..i])) 无论 class 列...
- 但是 Y[n.row,..i]<-sprintf("%s",as.character(Y[n.row ,..i])) returns
Error in
[<-.data.table
(*tmp*
, n.row, ..i, value = " Locataire de l'habitat social (OPH, OTHS)") : object '..i' not found
我不明白这里的行为。所有信息都可以一一找到,但我无法将一个信息分配给另一个信息,因为它突然找不到任何信息。
任何解释将不胜感激,或者我可能没有使用正确的策略来做我需要的事情:) !
感谢您的帮助!
注意 *tmp*
是一个字符,因此所有列都应保持相同的类型。
library(data.table)
Y <- fread("Statut occupation 1983 1988 1996 2002 2007 2012 2017
Propriétaire du logement 207 267 305 363 468 597 482
Locataire 35 40 33 52 50 61 60
Locataire de l'habitat social (OPH, OTHS) 0 0 0 0 0 2 0
Logé gratuitement (parents, amis, employeurs) 39 47 69 99 57 87 98
Total général 281 354 407 514 575 745 640",header = T,colClasses = 'character')
n.row <- 3
Y[n.row, names(Y) := as.list(sprintf("*%s*",Y[n.row,]))]
输出:
Statut occupation 1983 1988 1996 2002 2007 2012 2017
<char> <char> <char> <char> <char> <char> <char> <char>
1: Propriétaire du logement 207 267 305 363 468 597 482
2: Locataire 35 40 33 52 50 61 60
3: *Locataire de l'habitat social (OPH, OTHS)* *0* *0* *0* *0* *0* *2* *0*
4: Logé gratuitement (parents, amis, employeurs) 39 47 69 99 57 87 98
5: Total général 281 354 407 514 575 745 640