
[英]Loop through specific columns of dataframe keeping some columns as fixed
[英]Loop through specific columns in dataframe
我有一个要遍历的数据框,从每个数据框中打印出值...我是R的新手,对python和javascript更熟悉,所以我要对我尝试的内容进行伪编码在这里说:
df <- read.table(text="
a b c d e f g
1 5 6 7 3 4 7
3 2 8 4 1 9 4
6 5 2 5 4 6 1
",header=TRUE,stringsAsFactors = FALSE)
我的JavaScript心态...(我只想从第4列及以上进行选择):
for (i = 3; i < df.length; i++) {
console.log(i)
}
它会回到我身边
d e f g
7 3 4 7
4 1 9 4
5 4 6 1
为了更好地理解这一点,我正在制作一个仪表板,当我将鼠标悬停在某些数据点上时,工具提示中仅显示某些列中的值,这就是为什么我仍然需要保留原始数据框而不缩短它,同时编写算法的原因这将返回数据框中特定列的值,仅用于工具提示
这是两个子集一个数据帧的两种不同方式:
# by column numbers
df[, 4:7]
# by column names
names <- c("d", "e", "f", "g")
df[names]
您可以使用条件循环运行循环:
for (column in dataFrame) {
if(column %% 2 == 0){
for (entry in rawdata[,column]) {
#run your code here, it will impact ever second column
}
}
}
为了每个奇数列都将0更改为1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.