我有一个像这样的小数据框:
a<-c("green","green","green","green","green","green","green","green","green","green")
b<-c("blue","red","blue","red","blue","red","blue","red","blue","red")
df<-data.frame(a,b)
df
a b
1 green blue
2 green red
3 green blue
4 green red
5 green blue
6 green red
7 green blue
8 green red
9 green blue
10 green red
我想替换&#34;绿色&#34; by&#34; yellow&#34;其中b ==&#34;红色&#34;
我可以按df[b=="red",]
选择这些行,但替换不起作用。我试过了:
> df[b=="red",]$a<-gsub("green","yellow",df[b=="red",])
Fehler in `$<-.data.frame`(`*tmp*`, "a", value = c("c(1, 1, 1, 1, 1)", :
replacement has 2 rows, data has 5
或
> df$a[subset(df$a,df$b=="red")]<-"yellow"
Warnmeldung:
In `[<-.factor`(`*tmp*`, subset(df$a, df$b == "red"), value = c(NA, :
invalid factor level, NA generated
> df
a b
1 <NA> blue
2 green red
3 green blue
4 green red
5 green blue
6 green red
7 green blue
8 green red
9 green blue
10 green red
和
> df[b=="red",]$a<-"yellow"
Warnmeldung:
In `[<-.factor`(`*tmp*`, iseq, value = c("yellow", "yellow", "yellow", :
invalid factor level, NA generated
> df
a b
1 green blue
2 <NA> red
3 green blue
4 <NA> red
5 green blue
6 <NA> red
7 green blue
8 <NA> red
9 green blue
10 <NA> red
这对我的问题都不起作用。什么是最简单的解决方案?非常感谢,Treppenlift。
答案 0 :(得分:0)
您可以使用哪种功能。有关示例,请参阅http://www.endmemo.com/program/R/which.php。
rem // Let us assume variable `RelPath` holds the user input:
for %%I in ("%RelPath%") do set "EXT=%%~xI"
rem // Check whether the extension is missing:
if not defined EXT (
rem /* There is no extension, so let us assume the user input is a directory;
rem so let us append the default file name `test.js` in that case: */
set "RelPath=%RelPath%\test.js"
)
rem // Continue with the `dir`/`findstr` approach from use case 1 here...