课上小题

2023-03-23 12:34:12 浏览数 (1)

题1:

代码语言:txt复制
> x <- rep("a",3)
> y <- rep("b",3)
> #?如何让a和b在一起?
> paste0(x,y)#错了
[1] "ab" "ab" "ab"
> x y#错了,只能是数字
Error in x   y : non-numeric argument to binary operator
二进制运算符的非数字参数

> x&y #错了,只能是逻辑值
Error in x & y : 
  operations are possible only for numeric, logical or complex types
> merge(x,y) #错了,只能是数据框
  x y
1 a b
2 a b
3 a b
4 a b
5 a b
6 a b
7 a b
8 a b
9 a b
> 
> a <- 8:12
> a==10
[1] FALSE FALSE  TRUE FALSE FALSE
> a<12
[1]  TRUE  TRUE  TRUE  TRUE FALSE
> a==10&a<12
[1] FALSE FALSE  TRUE FALSE FALSE
> 
> c(x,y)
[1] "a" "a" "a" "b" "b" "b"

题2:

代码语言:txt复制
> x <- rep(c("a","b","c","d"),each=3)
> x
 [1] "a" "a" "a" "b" "b" "b" "c" "c" "c" "d" "d" "d"
> unique(x)
[1] "a" "b" "c" "d"
> #可以不用unique吗?
> duplicated(x)
 [1] FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE
> duplicated(!x)
Error in !x : invalid argument type
> !duplicated(x)
 [1]  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE
> x[!duplicated(x)]
[1] "a" "b" "c" "d"

题3:

代码语言:txt复制
> load("gands.Rdata")
> length(g)
[1] 100
选出向量g,中下标为偶数的基因名?
> g[seq(2,100,by=2)]
向量g中有多少个元素在向量s中存在?筛出来?
> table(g%in%s)                                g[g%in%s]
   FALSE  TRUE 
   37    63 
如果我用lintersect呢?不行!取交集函数会去重复

题4:

代码语言:txt复制
 test <- read.csv("exercise.csv")
> median(test$Petal.Length)
[1] 4.6
> class(test)
[1] "data.frame"
> str(test)
'data.frame':	15 obs. of  3 variables:
 $ Petal.Length: num  4.6 5.9 4.5 6 4 4.7 1.3 1.4 5.1 5.8 ...
 $ Petal.Width : num  1.5 2.1 1.5 2.5 1.3 1.4 0.2 0.2 1.9 2.2 ...
 $ Species     : chr  "a" "b" "a" "b" ...
> test[test$Species %in% c("a","c"),]
> test[test$Species==c("a","c")]
Error in `[.data.frame`(test, test$Species == c("a", "c")) : 
  选择了未定义的列
In addition: Warning message:
In test$Species == c("a", "c") :
  longer object length is not a multiple of shorter object length


# 为什么不行?因为会发生循环补齐 
test$Species
# [1] "a" "b" "a" "b" "a" "a" "c" "c" "b" "b" "a" "c" "c" "b" "c"
#和   "a" "c" "a" "c"  "a" "c" "a" "c" "a"  "c" "a" "c" "a" "c" "a" "c"

> test[test$Species==("a"|"c")]
Error in "a" | "c" : 
  operations are possible only for numeric, logical or complex types
#为什么不行?|符号值只支持 TRUE or FALSE

> test$Species=="a"
 [1]  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE
[13] FALSE FALSE FALSE
> test[test$Species=="a"|test$Species=="c",]

题5:

代码语言:txt复制
sort(x)[x>0]到底是什么意思??
回答:它按照x原本的T or F 去取 sort(x)的值

> x <- rnorm(10)
> x
 [1] -0.8969447 -0.4222281 -0.2335895 -0.1174351 -1.9966576  0.9978123 -0.8869931
 [8]  1.1003045 -0.2949218  1.2030764
> sort(x)
 [1] -1.9966576 -0.8969447 -0.8869931 -0.4222281 -0.2949218 -0.2335895 -0.1174351
 [8]  0.9978123  1.1003045  1.2030764
> sort(x)[x>0]
[1] -0.2335895  0.9978123  1.2030764
> x>0
 [1] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE  TRUE

上传失败:服务器响应格式错误

0 人点赞