💪 ผมขอแนะนำ R Book for Psychologists หนังสือสอนใช้ภาษา R เพื่อการวิเคราะห์ข้อมูลทางจิตวิทยา ที่เขียนมาเพื่อนักจิตวิทยาที่ไม่เคยมีประสบการณ์เขียน code มาก่อน
ในหนังสือ เราจะปูพื้นฐานภาษา R และพาไปดูวิธีวิเคราะห์สถิติที่ใช้บ่อยกัน เช่น:
Correlation
t-tests
ANOVA
Reliability
Factor analysis
🚀 เมื่ออ่านและทำตามตัวอย่างใน R Book for Psychologists ทุกคนจะไม่ต้องพึง SPSS และ Excel ในการทำงานอีกต่อไป และสามารถวิเคราะห์ข้อมูลด้วยตัวเองได้ด้วยความมั่นใจ
# Print when sunny
if weather == "sunny":
print("It's a sunny day. Don't forget your sunscreen!")
# Print when rainy
elif weather == "rainy":
print("It's raining. Remember to bring an umbrella!")
# Print when other conditions
else:
print("Likely chilly. Wear a jacket!")
ผลลัพธ์:
Likely chilly. Wear a jacket!
อธิบาย code:
if block: ประเมินว่า weather เป็น "sunny" ไหม ถ้าใช่ จะ print "It's a sunny day. Don't forget your sunscreen!"
elif block: weather เป็น "rainy" ไหม ถ้าใช่ จะ print "It's raining. Remember to bring an umbrella!"
# A shopping list program
shopping_list = ["milk", "bread", "chips", "apple", "toothpaste", "chocolate"]
# Loop through the list
for item in shopping_list:
# Skip item if chip
if item == "chips":
print("Chips are unhealthy. Skipping ...")
continue
# Stop the loop if toothpaste
if item == "toothpaste":
print("Found toothpaste, done shopping early!")
break
# Do nothing if milk
if item == "milk" or item == "bread":
pass
# Print item
print("Putting", item, "into the cart.")
ผลลัพธ์:
Putting milk into the cart.
Putting bread into the cart.
Chips are unhealthy. Skipping ...
Putting apple into the cart.
Found toothpaste, done shopping early!
time <- 10 # Start countdown
while (time > 0) {
print(paste("Counting down:", time))
time <- time - 1
}
ถ้าเราไม่ใส่ break, while loop ของเราจะนับเลขถึง 0:
while without break
.
แต่ถ้าเราใส่ break เข้าไป while loop จะหยุดนับ ณ ตัวเลขที่เรากำหนด:
time <- 10 # Start countdown
while (time > 0) {
if (time == 4) {
print("Countdown stopped.")
break # Stop the loop when time reaches 4
}
print(paste("Counting down:", time))
time <- time - 1
}
ผลลัพธ์:
while with break
จะเห็นได้ว่า break ทำให้ while loop หยุดทำงาน เมื่อนับถึง 4
💪 Summary
ในบทความนี้ เราเรียนรู้วิธีเขียน control flow ใน R กัน:
💪 ผมขอแนะนำ R Book for Psychologists หนังสือสอนใช้ภาษา R เพื่อการวิเคราะห์ข้อมูลทางจิตวิทยา ที่เขียนมาเพื่อนักจิตวิทยาที่ไม่เคยมีประสบการณ์เขียน code มาก่อน
ในหนังสือ เราจะปูพื้นฐานภาษา R และพาไปดูวิธีวิเคราะห์สถิติที่ใช้บ่อยกัน เช่น:
Correlation
t-tests
ANOVA
Reliability
Factor analysis
🚀 เมื่ออ่านและทำตามตัวอย่างใน R Book for Psychologists ทุกคนจะไม่ต้องพึง SPSS และ Excel ในการทำงานอีกต่อไป และสามารถวิเคราะห์ข้อมูลด้วยตัวเองได้ด้วยความมั่นใจ